一、首先准备jar包:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.5.0</version>
</dependency>
jedis是核心jar包。
二、redis.properties:
redis.pool.maxActive=100 //最大活动链接数
redis.pool.maxIdle=20//最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连
//接将被标记为不可用,然后被释放。设为0表示无限制。
redis.pool.maxWait=3000//最大等待时间
redis.ip=localhost //运行redis服务的ip
redis.port=6379 // redis服务的端口
三、RedisUtil.java:
public static JedisPool jedisPool; // 池化管理jedis链接池
static {
//从redis.propertise读取相关的配置
ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");
int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));
int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));
int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));
String ip = resourceBundle.getString("redis.ip");
int port = Integer.parseInt(resourceBundle.getString("redis.port"));
JedisPoolConfig config = new JedisPoolConfig();
//设置最大连接数
config.setMaxTotal(maxActive);
//设置最大空闲数
config.setMaxIdle(maxIdle);
//设置超时时间
config.setMaxWaitMillis(maxWait);
//初始化连接池
jedisPool = new JedisPool(config, ip, port);
}
//向缓存中设置字符串内容:
public static boolean set(String key,String value) throws Exception{
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
jedisPool.returnResource(jedis);
}
}
//向缓存中设置字符串内容
public static boolean set(String key,Object value){
Jedis jedis = null;
try {
String objectJson = JSON.toJSONString(value);
jedis = jedisPool.getResource();
jedis.set(key, objectJson);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
jedisPool.returnResource(jedis);
}
}
//根据key删除缓存中得对象
public static boolean del(String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.del(key);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
jedisPool.returnResource(jedis);
}
}
//根据key 获取内容
public static Object get(String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Object value = jedis.get(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
jedisPool.returnResource(jedis);
}
}
//根据key 获取对象
public static <T> T get(String key,Class<T> clazz){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String value = jedis.get(key);
return JSON.parseObject(value, clazz);
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
jedisPool.returnResource(jedis);
}
}
}
四、测试:
//保存对象
@Test
public void userCache(){
//向缓存中保存对象
UserDO jin = new UserDO();
jin.setUserId(1111);
jin.setSex(1);
jin.setUname("金金");
jin.setUnick("jin");
jin.setEmail("978954852@qq.com");
//调用方法处理
boolean reusltCache = RedisUtil.set("jin", jin);
if (reusltCache) {
System.out.println("向缓存中保存对象成功。");
}else{
System.out.println("向缓存中保存对象失败。");
}
}
获取对象
@Test
public void getUserInfo(){
UserDO jin = RedisUtil.get("jin",UserDO.class);
if(jin != null){
System.out.println("从缓存中获取的对象," + jin.getUname() + "@" + jin.getEmail());
}
}
这里只写出了一个String数据类型的操作,如果需要操作hash,List,set,sortset之类的,同理 封装一下hmset,hmget,lpush,lrange,sadd之类的方法到RedisUtil中,以后涉及到redis的操作, 就直接调用RedisUtil中的方法即可。 jedis的基本操作示例