Redis + Jedis + Spring
来源:互联网 发布:索尼网络电视机 编辑:程序博客网 时间:2024/06/04 18:05
参考一下这篇文章: http://snowolf.iteye.com/blog/1666908
也可以借鉴一下这个的思路,仅供参考:
package com.ttpod.cache.impl;
import com.ttpod.cache.Cache;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* date: 12-9-27 上午10:01
*
* @author: long.chen@ttpod.com
*/
public class RedisImpl implements Cache {
/**
* 1 day
*/
private static final int DEFAULT_TIME_TO_LIVE = 24 * 3600;
private int timeToLive = DEFAULT_TIME_TO_LIVE;
private StringRedisTemplate redisTemplate;
private final RedisSerializer<Object> objSer = new JdkSerializationRedisSerializer();
public void setTimeToLive(int timeToLive) {
this.timeToLive = timeToLive;
}
public void setRedisTemplate(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
public void set(String key, final Object value, final int expire) {
if(value instanceof Map){
redisTemplate.opsForHash().putAll(key, (Map) value );
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}else{
final byte[] rawKey = rawKey(key);
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.openPipeline();
connection.set(rawKey,objSer.serialize(value));
connection.expire(rawKey,expire);
return connection.closePipeline();
}
});
}
}
@Override
public void set(String key, Object value) {
set(key,value,timeToLive);
}
@Override
public Object get(String key) {
final byte[] rawKey = rawKey(key);
return redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
byte[] value = connection.get(rawKey);
return value == null ? null : objSer.deserialize(value);
}
});
}
@Override
public void delete(String key) {
redisTemplate.delete(key);
}
@Override
public void flushAll() {
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.flushAll();
return null;
}
});
}
private byte[] rawKey(String key) {
return redisTemplate.getStringSerializer().serialize(key);
}
}
- Redis + Jedis + Spring
- redis-jedis--spring-哨兵
- Spring Jedis集成Redis
- Spring + Jedis集成Redis
- Jedis+Spring操作Redis
- spring-data-redis + Jedis配置文件
- redis-jedis--spring-哨兵 3
- 使用Spring + Jedis集成Redis
- 使用Spring + Jedis集成Redis
- 使用Spring + Jedis集成Redis
- redis+Jedis+spring搭建简单web服务
- spring 注入 redis.clients.jedis.JedisPool 异常
- Redis客户端之Spring整合Jedis
- redis客户端jedis连接和spring结合
- 操作 redis-jedis--spring-哨兵 2
- Redis客户端之Spring整合Jedis
- Redis客户端之Spring整合Jedis
- Redis + Jedis + Spring整合遇到的异常
- 关于Apache (httpd)服务器防DDOS模块mod_evasive的使用说明
- Effective C++ 学记之20 宁以pass-by-reference-to-const 替换 pass-by-value
- ScrollView和ListView、GridView冲突解决
- Hibernate3.X—HelloWorld篇(入门篇)
- 方法与设计
- Redis + Jedis + Spring
- DataGridView的虚模式填充
- Fab CEO:我在创办4家公司中学到的90件事
- Android安卓Tabhost,TabActivity重影,残留破问题
- 如何写软件项目计划
- WP-首页缩略图效果
- 用C#创建Windows服务(Windows Services) 转自:http://www.cnblogs.com/caca/archive/2005/02/25/109028.html
- oracle学习笔记之数据查询
- usleep 和 sleep