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

來源:互聯網
上載者:User
 /**     * 從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 */@Componentpublic 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);    }


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.