您的位置:首页 > 数据库 > Redis

在spring data jpa中使用redis的通用list及entity存储方法

2014-02-11 11:27 801 查看
/**
* 从redis中获取对象。注意:未进行haskey检测
*
* @param e
* @param redis
* @param KEY
* @param KEY_LIST
* @param INDEX
* @return
* @version 1.0
* @since Service 1.0
* @date 2014年2月11日 上午10:59:06
*/
public static <E extends IdEntity> List<E> getList(Class<E> e, RedisClient redis, String KEY, String KEY_LIST, String INDEX) {
List<E> objects = Lists.newArrayList();
for (Long l : ListUtil.NotNullList(redis.getList(KEY_LIST + INDEX))) {
if (redis.hasKey(KEY + l)) {
objects.add(redis.getEntity(e, KEY + l));
}
}
return objects;
}

/**
* 在reids中注入缓存对象
*
* @param objects
* @param redis
* @param KEY
* @param KEY_LIST
* @param INDEX
* @return
* @version 1.0
* @since Service 1.0
* @date 2014年2月11日 上午10:59:50
*/
public static <E extends IdEntity> List<E> creatList(List<E> objects, RedisClient redis, String KEY, String KEY_LIST, String INDEX) {
List<E> list = ListUtil.NotNullList(objects);
for (E e : list) {
redis.addList(KEY_LIST + INDEX, e.getId());
redis.saveEntity(KEY, e);
}
return list;
}

/**
* 获取一个特定的实体类。如果redis中不存在,则从数据中获取
*
* @param e
* @param redis
* @param dao
* @param KEY
* @param id
* @return
* @version 1.0
* @since Service 1.0
* @date 2014年2月11日 上午11:00:21
*/
public static <E extends IdEntity> E getEntity(Class<E> e, RedisClient redis, CrudRepository<E, Long> dao, String KEY, Long id) {
if (redis.hasKey(KEY + id)) {
return BeanUtil.convertMap(e, redis.getHashOPS().entries(KEY + id));
} else {
synchronized (KEY) {
if (redis.hasKey(KEY + id)) {
return getEntity(e, redis, dao, KEY, id);
} else {
E entity = dao.findOne(id);
if (entity != null) {
redis.saveEntity(KEY, entity);
}
return entity;
}
}
}
}
/**
* 对RedisTemplate的封装
*
* @author 盼庚
* @version 1.0
* @since Service 1.0
* @date 2014年2月11日 上午11:31:00
* @control
*/
@Component
public class RedisClient {
@Autowired
private WebApplicationContext context;

public void expire(String key, Integer timeout) {
getRedis().expire(key, timeout, TimeUnit.MINUTES);
}

@SuppressWarnings("unchecked")
public RedisTemplate<Serializable, Serializable> getRedis() {
return (RedisTemplate<Serializable, Serializable>) context.getBean("redisTemplate");
}

@SuppressWarnings("unchecked")
public <V> RedisTemplate<String, V> getRedis(Class<V> v) {
return (RedisTemplate<String, V>) context.getBean("redisTemplate");
}

@SuppressWarnings("unchecked")
public <V, K> RedisTemplate<K, V> getRedis(Class<K> k, Class<V> v) {
return (RedisTemplate<K, V>) context.getBean("redisTemplate");
}

public <K, V> HashOperations<K, String, V> getHashOPS(Class<K> k, Class<V> v) {
return getRedis(k, v).opsForHash();
}

public <T> HashOperations<String, Object, T> getHashOPS(Class<T> T) {
return getRedis(Object.class).opsForHash();
}

public HashOperations<String, Object, Object> getHashOPS() {
return getRedis(Object.class).opsForHash();
}

public HashOperations<String, Object, String> getHashOPSByString() {
return getRedis(Object.class).opsForHash();
}

public HashOperations<String, Object, Long> getHashOPSByLong() {
return getRedis(Object.class).opsForHash();
}

public <V> ListOperations<String, V> getListOPS(Class<V> v) {
return getRedis(v).opsForList();
}

public <V> ListOperations<String, Long> getListOPSByLong() {
return getRedis(Long.class).opsForList();
}

public <V> SetOperations<String, V> getSetOPS(Class<V> v) {
return getRedis(v).opsForSet();
}

public <V> ValueOperations<String, V> getValueOPS(Class<V> v) {
return getRedis(v).opsForValue();
}

public ValueOperations<String, Object> getObject(String key) {
return getRedis(Object.class).opsForValue();
}

public ValueOperations<String, String> getString(String key) {
return getRedis(String.class).opsForValue();
}

public ValueOperations<String, Long> getLong(String key) {
return getRedis(Long.class).opsForValue();
}

public <V> ZSetOperations<String, V> getZSetOPS(Class<V> v) {
return getRedis(v).opsForZSet();
}

public <T extends IdEntity> void saveEntity(String key, T t) {
getHashOPS().putAll(key + t.getId(), BeanUtil.convertBean(t));
}

public void addList(String key, Long id) {
getListOPSByLong().rightPush(key, id);
}

public List<Long> getList(String key) {
return getListOPSByLong().range(key, 0, -1);
}

public <T extends IdEntity> T getEntity(Class<T> t, String key) {
return BeanUtil.convertMap(t, getHashOPS().entries(key));
}

public Boolean hasKey(String key) {
return getRedis().hasKey(key);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: