Redis缓存学习与使用(基于struts2+spring+mybatis)

Redis 简介(copy菜鸟教程)

Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库。

Redis 与其他 key - value 缓存产品有以下三个特点:

  • Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
  • Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
  • Redis支持数据的备份,即master-slave模式的数据备份。

Redis支持五种数据类型,string,hash,list,set,zset

接下来就是我在使用框架时为了给基础数据添加redis缓存工具,进行了redis的基础配置和工具类的编写。

1.基于ssm框架配置redis(此处简单概括,详细配置请参考链接http://www.cnblogs.com/cuglkb/p/6862609.html)

  一、编写redis.properties文件

  二、编写applicationContext-redis.xml文件,配置好redis相关的连接池等

    注意如果要写工具类的话要实例化bean,不然会在服务器启动时报错

  三、在spring的配置文件applicationContext.xml中添加读取redis.properties和applicationContext-redis.xml

  四、现在redis的基本配置已经初步完成啦,接下来就是写redis的工具类了(我的工具类名是RedisCacheutil)

    因为我是在上述的applicationContext-redis.xml中配置了工具类的bean,所以给我定义的redisTemplate添加getter和setter

  1 public class RedisCacheUtil<T> {
  2     
  3     @SuppressWarnings("rawtypes")
  4     public  RedisTemplate redisTemplate;
  5     
  6     /**
  7      * 
  8     * @Title: exists 
  9     * @Description: 判断缓存中是否存有对应key的value
 10     * @param @param key
 11     * @param @return    设定文件 
 12     * @return boolean    返回类型 
 13     * @throws
 14      */
 15     public boolean exists(final String key) {
 16         return redisTemplate.hasKey(key);
 17     }
 18     
 19     /**
 20      * 
 21     * @Title: setObject 
 22     * @Description: 缓存基本对象
 23     * @param @param key
 24     * @param @param value
 25     * @param @return    设定文件 
 26     * @return ValueOperations<String,T>    返回类型 
 27     * @throws
 28      */
 29     public   <T> ValueOperations<String, T> setObject(String key,T value){
 30         ValueOperations<String, T> operations = redisTemplate.opsForValue();
 31         operations.set(key, value);
 32         return operations;
 33     }
 34     
 35     /**
 36      * 
 37     * @Title: getObject 
 38     * @Description: 获得缓存的基本对象 
 39     * @param @param key
 40     * @param @return    设定文件 
 41     * @return T    返回类型 
 42     * @throws
 43      */
 44     public   <T> T getObject(String key){
 45         ValueOperations<String, T> operations = redisTemplate.opsForValue();
 46         return operations.get(key);
 47     }
 48     
 49     /**
 50      * 
 51     * @Title: setList 
 52     * @Description: 缓存List 
 53     * @param @param key
 54     * @param @param dataList
 55     * @param @return    设定文件 
 56     * @return ListOperations<String,T>    返回类型 
 57     * @throws
 58      */
 59     public <T> ListOperations<String, T> setList(String key , List<T> dataList){
 60         ListOperations<String, T> listOperations = redisTemplate.opsForList();
 61         if(dataList != null && dataList.size()>0){
 62             int size = dataList.size();
 63             for(int i=0;i<size;i++){
 64                 listOperations.rightPush(key, dataList.get(i));
 65             }
 66         }
 67         return listOperations;
 68     }
 69     
 70     /**
 71      * 
 72     * @Title: getList 
 73     * @Description: 获得缓存list
 74     * @param @param key
 75     * @param @return    设定文件 
 76     * @return List<T>    返回类型 
 77     * @throws
 78      */
 79     @SuppressWarnings("unchecked")
 80     public   <T> List<T> getList(String key){
 81         List<T> dataList = new ArrayList<T>();
 82         ListOperations<String,T> listOperation = redisTemplate.opsForList();
 83         Long size = listOperation.size(key);
 84         for(int i = 0 ; i < size ; i ++){
 85             dataList.add((T) listOperation.range(key, 0, -1));
 86         }
 87         return dataList;
 88     }
 89     
 90     /**
 91      * 
 92     * @Title: setSet 
 93     * @Description: 缓存set 
 94     * @param @param key
 95     * @param @param dataSet
 96     * @param @return    设定文件 
 97     * @return BoundSetOperations<String,T>    返回类型 
 98     * @throws
 99      */
100     @SuppressWarnings("unchecked")
101     public   <T> BoundSetOperations<String,T> setSet(String key,Set<T> dataSet){
102         BoundSetOperations<String,T> setOperation = redisTemplate.boundSetOps(key); 
103           
104         Iterator<T> it = dataSet.iterator();
105         while(it.hasNext()){
106             setOperation.add(it.next());
107         }
108         return setOperation;
109     }
110     
111     /**
112      * 
113     * @Title: getSet 
114     * @Description: 获得缓存set
115     * @param @param key
116     * @param @return    设定文件 
117     * @return Set<T>    返回类型 
118     * @throws
119      */
120     public Set<T> getSet(String key){
121         Set<T> dataSet = new HashSet<T>();
122         @SuppressWarnings("unchecked")
123         BoundSetOperations<String,T> operation = redisTemplate.boundSetOps(key); 
124         Long size = operation.size();
125         for(int i = 0 ; i < size ; i++){
126             dataSet.add(operation.pop());
127         }
128         return dataSet;
129     }
130     
131     /**
132      * 
133     * @Title: setMap 
134     * @Description: 缓存map
135     * @param @param key
136     * @param @param dataMap
137     * @param @return    设定文件 
138     * @return HashOperations<String,String,T>    返回类型 
139     * @throws
140      */
141     @SuppressWarnings("unchecked")
142     public <T> HashOperations<String,String,T> setMap(String key,Map<String,T> dataMap){
143        @SuppressWarnings("rawtypes")
144     HashOperations hashOperations = redisTemplate.opsForHash();
145        if(null != dataMap){
146            for (Map.Entry<String, T> entry : dataMap.entrySet()) {   
147         /*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); */
148                hashOperations.put(key,entry.getKey(),entry.getValue());
149            } 
150         
151        }
152        return hashOperations;
153     }
154     
155     /**
156      * 
157     * @Title: getMap 
158     * @Description: 获得缓存map
159     * @param @param key
160     * @param @return    设定文件 
161     * @return Map<String,T>    返回类型 
162     * @throws
163      */
164     public <T> Map<String,T> getMap(String key){
165       Map<String, T> map = redisTemplate.opsForHash().entries(key);
166       return map;
167      }
168 
169     public RedisTemplate getRedisTemplate() {
170         return redisTemplate;
171     }
172 
173     public void setRedisTemplate(RedisTemplate redisTemplate) {
174         this.redisTemplate = redisTemplate;
175     }

  五、工具类写完,要用的话是在service层进行使用,所以在需要用到redis缓存工具类的地方注入redis

@Resource
    private RedisCacheUtil<DataDictionary> redisCacheUtil;

  六、然后就是在service中的方法中使用RedisCacheUtil的方法。

  七、最后一步,在action中进行测试,其实就是把对应service注入进来调用一下方法

  八、测试的话,可以在action中打一个断点,然后再在service中打一个断点然后F6一步一步跑,去看看是不是与数据库交互查询

   

还有Redis-Server不要忘了开,不然服务器启动还是会报错的。

转载于:https://www.cnblogs.com/yangliuCode/p/7453329.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值