目前的项目中使用到了Jedis,剥离了客户端代码用以测试学习。
UserBean.java 实体类,可无视
package com.redis.bean;
public class UserBean {
private String key;
private long uid;
private String name;
public long getUid() {
return uid;
}
public void setUid(long uid) {
this.uid = uid;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "UserBean [key=" + key + ", uid=" + uid + ", name=" + name + "]";
}
}
RedisPool.java 连接单例类
package com.redis.storage;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisPool {
private static RedisPool instance = new RedisPool();
private static JedisPool jedisPool = null;
private static final String REDIS_HOST = "127.0.0.1";
private static final int REDIS_PORT = 6379; //redis默认端口
// 单例
private RedisPool() {
}
public static final RedisPool getInstance() {
return instance;
}
// 初始化
public void Init() {
if (jedisPool == null) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(100);
config.setMaxIdle(20);
config.setMaxWait(1000);
/**如果为true,表示有一个idle object evitor线程对idle object进行扫描,
* 如果validate失败,此object会被从pool中drop掉;
* 这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义;**/
//config.setTestWhileIdle(true);
/**表示一个对象至少停留在idle状态的最短时间,然后才能被idle object evitor扫描并驱逐;
* 这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义;**/
//config.setMinEvictableIdleTimeMillis(60000);
/**表示idle object evitor两次扫描之间要sleep的毫秒数**/
//config.setTimeBetweenEvictionRunsMillis(30000);
/**表示idle object evitor每次扫描的最多的对象数**/
//config.setNumTestsPerEvictionRun(-1);
jedisPool = new JedisPool(config, REDIS_HOST, REDIS_PORT);
}
}
// 取出redis连接
public synchronized Jedis getConnection() throws Exception {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
} catch (Exception e) {
System.out.println("[getConnection]" + " error:" + e.getMessage());
if (jedis != null) {
jedisPool.returnBrokenResource(jedis);
}
e.printStackTrace();
throw new Exception(e);
}
return jedis;
}
// 回收redis连接
public synchronized void releaseConnetion(Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
UserDAO.java 数据操作
package com.redis.DAO;
import java.util.HashMap;
import java.util.Map;
import redis.clients.jedis.Jedis;
import com.redis.bean.UserBean;
import com.redis.storage.RedisPool;
public class UserDAO {
private static final String U_UID = "uid";
private static final String U_KEY = "gen_sn";
private static final String U_NAME = "name";
private static final String U_PROFILE_PRE = "pr:";
public UserBean getUserFromRedis(String genSN) throws Exception {
UserBean userBean = null;
Jedis jedis = null;
try {
jedis = RedisPool.getInstance().getConnection();
Map<String, String> userProfile = null;
userProfile = jedis.hgetAll(U_PROFILE_PRE + genSN);
if (null != userProfile && userProfile.size() != 0) {
userBean = new UserBean();
userBean.setUid(Long.parseLong(userProfile.get(U_UID)));
userBean.setKey(userProfile.get(U_KEY));
userBean.setName(userProfile.get(U_NAME));
}
} catch (NumberFormatException e) {
throw new Exception(e);
} finally {
RedisPool.getInstance().releaseConnetion(jedis);
}
return userBean;
}
public void addUserIntoRedis(UserBean userBean) throws Exception {
Jedis jedis = null;
try {
jedis = RedisPool.getInstance().getConnection();
HashMap<String, String> profileMap = new HashMap<String, String>();
profileMap.put(U_UID, Long.toString(userBean.getUid()));
if (userBean.getKey() != null) {
profileMap.put(U_KEY, userBean.getKey());
}
if (userBean.getName() != null) {
profileMap.put(U_NAME, userBean.getName());
}
// 放入整个map
jedis.hmset(U_PROFILE_PRE + userBean.getKey(), profileMap);
} catch (Exception e) {
throw new Exception(e);
} finally {
RedisPool.getInstance().releaseConnetion(jedis);
}
}
}
Main.java
package com.redis.service;
import com.redis.DAO.UserDAO;
import com.redis.bean.UserBean;
import com.redis.storage.RedisPool;
public class Main {
public static void main(String[] args) throws Exception {
RedisPool.getInstance().Init();
UserDAO dao=new UserDAO();
UserBean user=new UserBean();
user.setKey("test"); //唯一标识
user.setUid(1000);
user.setName("卓东来");
dao.addUserIntoRedis(user);
UserBean result=dao.getUserFromRedis("test");
System.out.println(result.getUid());
System.out.println(result.getName());
}
}