首页 > 代码库 > redis的简单使用实例
redis的简单使用实例
1:在工程中导入一下两个jar包
2:
Javabean
package com.eoopen.xmpp.loginin;import java.io.Serializable;import java.util.Date;public class ClientUser implements Serializable{ /** * 序列化 */ private static final long serialVersionUID = 2029803928604452271L; public ClientUser() { } public ClientUser(String clientIdentify) { super(); this.clientIdentify = clientIdentify; } /** * 重写hashcode * clientIdentify相同就代表是同一个对象 */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((clientIdentify == null) ? 0 : clientIdentify.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ClientUser other = (ClientUser) obj; if (clientIdentify == null) { if (other.clientIdentify != null) return false; } else if (!clientIdentify.equals(other.clientIdentify)) return false; return true; } private String id; private String clientuserid; /** * 每个客户端的唯一标识(如格式为111223_359776051195305), * 该参数需要客户端组装后传给服务端, * 组装方式为uid+设备唯一标识, * 该字段最终作为客户端登录openfire的账号, * 客户端登陆的时候openfire的时候,账号为clientIdentity, * 密码和客户端登陆密码一致 */ private String clientIdentify; // 系统标识,如eim,lbs private String serveridentify; // 注册openfire账号时间 private Date createtime; private Date updatetime; // 来自哪个客户端,ios,Android,pc private String clienttype; public String getClienttype() { return clienttype; } public void setClienttype(String clienttype) { this.clienttype = clienttype; } public Date getUpdatetime() { return updatetime; } public void setUpdatetime(Date updatetime) { this.updatetime = updatetime; } public String getServeridentify() { return serveridentify; } public void setServeridentify(String serveridentify) { this.serveridentify = serveridentify; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getClientuserid() { return clientuserid; } public void setClientuserid(String clientuserid) { this.clientuserid = clientuserid; } public String getClientIdentify() { return clientIdentify; } public void setClientIdentify(String clientIdentify) { this.clientIdentify = clientIdentify; }}
(2)示例
package com.eo.test.redis;import java.util.Set;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.SetOperations;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.eoopen.xmpp.loginin.ClientUser;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:applicationContext.xml")public class TestZWJ extends AbstractJUnit4SpringContextTests{ @Autowired private RedisTemplate redisTemplate; @SuppressWarnings("unchecked") @Test public void testOfSet(){ // 创建一个set队列
SetOperations<String, ClientUser> opsForSet = redisTemplate.opsForSet(); ClientUser u1 = new ClientUser("11"); ClientUser u2 = new ClientUser("12"); ClientUser u3 = new ClientUser("13"); ClientUser u4 = new ClientUser("14"); // 往队列中添加4个用户
opsForSet.add("66666666", u1); opsForSet.add("66666666", u2); opsForSet.add("66666666", u3); opsForSet.add("66666666", u4);
// 取出队列中的所有对象
Set<ClientUser> members = opsForSet.members("66666666");
// 循环打印队列中的所有对象
for (ClientUser clientUser : members) { System.out.println(clientUser.getClientIdentify()); } // 删除队列中的一条数据
ClientUser u5 = new ClientUser("14"); members.remove(u5); for (ClientUser clientUser : members) { System.out.println(clientUser.getClientIdentify()); } Set<ClientUser> members2 = opsForSet.members("66666666"); for (ClientUser clientUser : members2) { System.out.println(clientUser.getClientIdentify()); } }}
3:将数据库中的数据全部放入redis缓存中
public void initUserCacheQueue(){ RedisTemplate redisTemplate = (RedisTemplate) ContextUtil.getBean("redisTemplate"); //清空集合 redisTemplate.delete(Constant.OPENFIRE_USERS); redisTemplate.delete(Constant.CLIENT_USERS); ClientUserService userService = (ClientUserService) ContextUtil.getBean("clientUserService"); // 将所有的ClientUser数据放入redis缓存中 List<ClientUser> clientUsers = userService.queryClientUser(); for (ClientUser clientUser : clientUsers) { redisTemplate.opsForSet().add(Constant.CLIENT_USERS, clientUser); } // 将所有的OfUser数据放入redis缓存中 List<OfUser> opeOfUsers = userService.queryOpenfireUser(); for (OfUser ofUser : opeOfUsers) { redisTemplate.opsForSet().add(Constant.OPENFIRE_USERS, ofUser); } }
redis的简单使用实例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。