在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  List getList(Class e, RedisClient redis, String KEY, String KEY_LIST, String INDEX) {
        List 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  List creatList(List objects, RedisClient redis, String KEY, String KEY_LIST, String INDEX) {
        List 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 getEntity(Class e, RedisClient redis, CrudRepository 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 getRedis() {
        return (RedisTemplate) context.getBean("redisTemplate");
    }

    @SuppressWarnings("unchecked")
    public  RedisTemplate getRedis(Class v) {
        return (RedisTemplate) context.getBean("redisTemplate");
    }

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

    public  HashOperations getHashOPS(Class k, Class v) {
        return getRedis(k, v).opsForHash();
    }

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

    public HashOperations getHashOPS() {
        return getRedis(Object.class).opsForHash();
    }

    public HashOperations getHashOPSByString() {
        return getRedis(Object.class).opsForHash();
    }

    public HashOperations getHashOPSByLong() {
        return getRedis(Object.class).opsForHash();
    }

    public  ListOperations getListOPS(Class v) {
        return getRedis(v).opsForList();
    }

    public  ListOperations getListOPSByLong() {
        return getRedis(Long.class).opsForList();
    }

    public  SetOperations getSetOPS(Class v) {
        return getRedis(v).opsForSet();
    }

    public  ValueOperations getValueOPS(Class v) {
        return getRedis(v).opsForValue();
    }

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

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

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

    public  ZSetOperations getZSetOPS(Class v) {
        return getRedis(v).opsForZSet();
    }

    public  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 getList(String key) {
        return getListOPSByLong().range(key, 0, -1);
    }

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

    public Boolean hasKey(String key) {
        return getRedis().hasKey(key);
    }


你可能感兴趣的:(Tools)