打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
redis 几种数据类型往数据库存数据和取数据的帮助类
package com.fndsoft.bcis.utils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.*;import org.springframework.stereotype.Service;import java.util.*;/** * redis缓存帮助类 * Created by DELL on 2016/5/23. */@Servicepublic class RedisCacheUtil<T> {    @Autowired    public RedisTemplate redisTemplate;    /**     * 缓存基本的对象,Integer、String、实体类等     *     * @param key   缓存的键值     * @param value 缓存的值     * @return 缓存的对象     */    public <T> ValueOperations<String, T> setCacheObject(String key, T value) {        ValueOperations<String, T> operation = redisTemplate.opsForValue();        operation.set(key, value);        return operation;    }    /**     * 获得缓存的基本对象。     *     * @param key 缓存键值     * @return 缓存键值对应的数据     */    public <T> T getCacheObject(String key) {        ValueOperations<String, T> operation = redisTemplate.opsForValue();        return operation.get(key);    }    /**     * 缓存List数据     *     * @param key      缓存的键值     * @param dataList 待缓存的List数据     * @return 缓存的对象     */    public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList) {        ListOperations listOperation = redisTemplate.opsForList();        if (null != dataList) {            int size = dataList.size();            for (int i = 0; i < size; i++) {                listOperation.leftPush(key, dataList.get(i));            }        }        return listOperation;    }    /**     * 获得缓存的list对象     *     * @param key 缓存的键值     * @return 缓存键值对应的数据     */    public <T> List<T> getCacheList(String key) {        List<T> dataList = new ArrayList<T>();        ListOperations<String, T> listOperation = redisTemplate.opsForList();        Long size = listOperation.size(key);        for (int i = 0; i < size; i++) {            dataList.add(listOperation.index(key,i));        }        return dataList;    }    /**     * 缓存Set     *     * @param key     缓存键值     * @param dataSet 缓存的数据     * @return 缓存数据的对象     */    public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet) {        BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);        Iterator<T> it = dataSet.iterator();        while (it.hasNext()) {            setOperation.add(it.next());        }        return setOperation;    }    /**     * 获得缓存的set     *     * @param key     * @return     */    public Set<T> getCacheSet(String key) {        Set<T> dataSet = new HashSet<T>();        BoundSetOperations<String, T> operation = redisTemplate.boundSetOps(key);        Long size = operation.size();        for (int i = 0; i < size; i++) {            dataSet.add(operation.pop());        }        return dataSet;    }    /**     * 缓存Map     *     * @param key     * @param dataMap     * @return     */    public <T> HashOperations<String, String, T> setCacheMap(String key, Map<String, T> dataMap) {        HashOperations hashOperations = redisTemplate.opsForHash();        if (null != dataMap) {            for (Map.Entry<String, T> entry : dataMap.entrySet()) {                hashOperations.put(key, entry.getKey(), entry.getValue());            }        }        return hashOperations;    }    /**     * 获得缓存的Map     *     * @param key     * @return     */    public <T> Map<String, T> getCacheMap(String key) {        Map<String, T> map = redisTemplate.opsForHash().entries(key);        return map;    }    /**     * 缓存Map     *     * @param key     * @param dataMap     * @return     */    public <T> HashOperations<String, Integer, T> setCacheIntegerMap(String key, Map<Integer, T> dataMap) {        HashOperations hashOperations = redisTemplate.opsForHash();        if (null != dataMap) {            for (Map.Entry<Integer, T> entry : dataMap.entrySet()) {                hashOperations.put(key, entry.getKey(), entry.getValue());            }        }        return hashOperations;    }    /**     * 获得缓存的Map     *     * @param key     * @return     */    public <T> Map<Integer, T> getCacheIntegerMap(String key) {        Map<Integer, T> map = redisTemplate.opsForHash().entries(key);        return map;    }}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
spring结合redis如何实现数据的缓存
Redis整合Spring结合使用缓存实例
java架构师-技术专家-练习手册-第07周 分布式redis
java 用redis如何处理电商平台,秒杀、抢购超卖
springboot之使用redistemplate优雅地操作redis
Java封装redis工具类
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服