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

 /**
     * 从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);
    }


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值