发表于: 2019-05-28 22:25:57

2 565


今天完成的事情

今天开始任务六的学习,学习Memcache和Redis缓存技术

 

首先学习了Redis缓存技术

1.     Redis缓存技术是基于KV键值对的内容缓存

2.     使用hash table实现kv键值对的存储

3.     数据无结构化,数据存储需要序列化为字符串或者二进制数据

 

Redis缓存技术的配置

1.     Redis数据连接池配置(Spring.xml)

2.     Jedis工厂模型(Spring.xml),用于Java客户端封装调用Redis的API

3.     RedisTemplate工具类,封装缓存的使用方法

 

具体代码如下

1.     Redis数据连接池配置

<!--JedisPoolConfig对象-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
   <!--最大空闲数-->
   <property name="maxIdle" value="50"/>
   <!--最大连接数-->
   <property name="maxTotal" value="100"/>
   <!--最大等待时间-->
   <property name="maxWaitMillis" value="20000"/>
</bean>


2.     Jedis工厂模型

<!--Jedis工厂模型-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
   <!--服务器-->
   <property name="hostName" value="localhost"/>

   <!--&lt;!&ndash;接口端口&ndash;&gt;-->
   <!--<property name="port" value="6379"/>-->
   <!--<property name="password" value="password"/>-->

   <!--连接池配置对象-->
   <property name="poolConfig" ref="poolConfig"/>
</bean>


3.   Jedis序列化

<!--Jedis序列化-->
<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
   <property name="connectionFactory" ref="connectionFactory"/>
   <property name="keySerializer" ref="stringRedisSerializer"/>
   <property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>


4.     RedisTemplate工具类

 

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
基于springredisredisTemplate具类
针对所有的hash 都是以h开头的
针对所有的Set 都是以s开头的法 不含通⽤⽅
针对所有的List 都是以l开头的
*/
public class RedisUtil {
   private RedisTemplate<StringObjectredisTemplate;
   public void setRedisTemplate(RedisTemplate<StringObjectredisTemplate) {
       this.redisTemplate redisTemplate;
   }
   //=============================common============================
   /**
    * 指定缓存失效时间
    @param key 
    @param time 时间()
    * @return
    */
   public boolean expire(String key,long time){
       try {
           if(time>0){
               redisTemplate.expire(keytimeTimeUnit.SECONDS);
           }
           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * 根据key 获取过期时间
    @param key 键 不能为null
    * @return 时间(返回0代表为永久有效
    */
   public long getExpire(String key){
       return redisTemplate.getExpire(key,TimeUnit.SECONDS);
   }
   /**
    * 判断key是否存在
    @param key 
    @return true 存在 false不存在
    */
   public boolean hasKey(String key){
       try {
           return redisTemplate.hasKey(key);
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * 删除缓存
    @param key 可以传个值 或多个
    */
   @SuppressWarnings("unchecked")
   public void del(String ... key){
       if(key!=null&&key.length>0){
           if(key.length==1){
               redisTemplate.delete(key[0]);
           }else{
               redisTemplate.delete(CollectionUtils.arrayToList(key));
           }
       }
   }
   //============================String=============================
   /**
    * 普通缓存获取
    @param key 
    @return 
    */
   public Object get(String key){
       return key==null?null:redisTemplate.opsForValue().get(key);
   }
   /**
    * 普通缓存放
    @param key 
    @param value 
    @return true成功 false失败
    */
   public boolean set(String key,Object value) {
       try {
           redisTemplate.opsForValue().set(keyvalue);
           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * 普通缓存放并设置时间
    @param key 
    @param value 
    @param time 时间() time如果time于等于将设置限期
    @return true成功 false 失败
    */
   public boolean set(String key,Object value,long time){
       try {
           if(time>0){
               redisTemplate.opsForValue().set(keyvaluetimeTimeUnit.SECONDS);
           }else{
               set(keyvalue);
           }
           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * 递增
    @param key 
    @param delta 要增加(0)
    * @return
    */
   public long incr(String keylong delta){
       if(delta<0){
           throw new RuntimeException("递增因必须0");
       }
       return redisTemplate.opsForValue().increment(keydelta);
   }
   /**
    * 递减
    @param key 
    @param delta 要减少(0)
    * @return
    */
   public long decr(String keylong delta){
       if(delta<0){
           throw new RuntimeException("递减因必须0");
       }
       return redisTemplate.opsForValue().increment(key, -delta);
   }
   //================================Map=================================
   /**
    * HashGet
    * @param key 键 不能为null
    * @param item 项 不能为null
    * @return 
    */
   public Object hget(String key,String item){
       return redisTemplate.opsForHash().get(keyitem);
   }
   /**
    * 获取hashKey对应的所有键值
    @param key 
    @return 对应的多个键值
    */
   public Map<Object,Objecthmget(String key){
       return redisTemplate.opsForHash().entries(key);
   }
   /**
    * HashSet
    * @param key 
    @param map 对应多个键值
    @return true 成功 false 失败
    */
   public boolean hmset(String keyMap<String,Objectmap){
       try {
           redisTemplate.opsForHash().putAll(keymap);
           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * HashSet 并设置时间
    @param key 
    @param map 对应多个键值
    @param time 时间()
    * @return true成功 false失败
    */
   public boolean hmset(String keyMap<String,Objectmaplong time){
       try {
           redisTemplate.opsForHash().putAll(keymap);
           if(time>0){
               expire(keytime);
           }
           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * hash表中放数据,如果不存在将创建
    @param key 
    @param item 
    @param value 
    @return true 成功 false失败
    */
   public boolean hset(String key,String item,Object value) {
       try {
           redisTemplate.opsForHash().put(keyitemvalue);
           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * hash表中放数据,如果不存在将创建
    @param key 
    @param item 
    @param value 
    @param time 时间(注意:如果已存在的hash表有时间,将会替换原有的时间
    @return true 成功 false失败
    */
   public boolean hset(String key,String item,Object value,long time) {
       try {
           redisTemplate.opsForHash().put(keyitemvalue);
           if(time>0){
               expire(keytime);
           }
           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * 删除hash表中的值
    @param key 键 不能为null
    * @param item 项 可以使多个 不能为null
    */
   public void hdel(String keyObject... item){
       redisTemplate.opsForHash().delete(key,item);
   }
   /**
    * 判断hash表中是否有该项的值
    @param key 键 不能为null
    * @param item 项 不能为null
    * @return true 存在 false不存在
    */
   public boolean hHasKey(String keyString item){
       return redisTemplate.opsForHash().hasKey(keyitem);
   }
   /**
    * hash递增 如果不存在,就会创建个 并把新增后的值返回
    @param key 
    @param item 
    @param by 要增加(0)
    * @return
    */
   public double hincr(String keyString item,double by){
       return redisTemplate.opsForHash().increment(keyitemby);
   }
   /**
    * hash递减
    @param key 
    @param item 
    @param by 要减少记(0)
    * @return
    */
   public double hdecr(String keyString item,double by){
       return redisTemplate.opsForHash().increment(keyitem,-by);
   }
   //============================set=============================
   /**
    * 根据key获取Set中的所有值
    @param key 
    @return
    */
   public Set<ObjectsGet(String key){
       try {
           return redisTemplate.opsForSet().members(key);
       } catch (Exception e) {
           e.printStackTrace();
           return null;
       }
   }
   /**
    * 根据valueset中查询,是否存在
    @param key 
    @param value 
    @return true 存在 false不存在
    */
   public boolean sHasKey(String key,Object value){
       try {
           return redisTemplate.opsForSet().isMember(keyvalue);
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * 将数据放set缓存
    @param key 
    @param values 值 可以是多个
    @return 成功个数
    */
   public long sSet(String keyObject...values) {
       try {
           return redisTemplate.opsForSet().add(keyvalues);
       } catch (Exception e) {
           e.printStackTrace();
           return 0;
       }
   }
   /**
    * set数据放缓存
    @param key 
    @param time 时间()
    * @param values 值 可以是多个
    @return 成功个数
    */
   public long sSetAndTime(String key,long time,Object...values) {
       try {
           Long count redisTemplate.opsForSet().add(keyvalues);
           if(time>0) {
               expire(keytime);
           }
           return count;
       } catch (Exception e) {
           e.printStackTrace();
           return 0;
       }
   }
   /**
    * 获取set缓存的
    @param key 
    @return
    */
   public long sGetSetSize(String key){
       try {
           return redisTemplate.opsForSet().size(key);
       } catch (Exception e) {
           e.printStackTrace();
           return 0;
       }
   }
   /**
    * 移除值为value
    @param key 
    @param values 值 可以是多个
    @return 移除的个数
    */
   public long setRemove(String keyObject ...values) {
       try {
           Long count redisTemplate.opsForSet().remove(keyvalues);
           return count;
       } catch (Exception e) {
           e.printStackTrace();
           return 0;
       }
   }
   //===============================list=================================
   /**
    * 获取list缓存的内容
    @param key 
    @param start 开始
    @param end 结束 到 -1代表所有值
    @return
    */
   public List<ObjectlGet(String key,long startlong end){
       try {
           return redisTemplate.opsForList().range(keystartend);
       } catch (Exception e) {
           e.printStackTrace();
           return null;
       }
   }
   /**
    * 获取list缓存的
    @param key 
    @return
    */
   public long lGetListSize(String key){
       try {
           return redisTemplate.opsForList().size(key);
       } catch (Exception e) {
           e.printStackTrace();
           return 0;
       }
   }
   /**
    * 通过索引 获取list中的值
    @param key 
    @param index 索引 index>=0时, 表头,个元素,依次类推;index<0时,-1,表尾,-2倒数第个元素,依次类推
    @return
    */
   public Object lGetIndex(String key,long index){
       try {
           return redisTemplate.opsForList().index(keyindex);
       } catch (Exception e) {
           e.printStackTrace();
           return null;
       }
   }
   /**
    * list缓存
    @param key 
    @param value 
    @return
    */
   public boolean lSet(String keyObject value) {
       try {
           redisTemplate.opsForList().rightPush(keyvalue);
           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * list缓存
    @param key 
    @param value 
    @param time 时间()
    * @return
    */
   public boolean lSet(String keyObject valuelong time) {
       try {
           redisTemplate.opsForList().rightPush(keyvalue);
           if (time 0) {
               expire(keytime);
           }
           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * list缓存
    @param key 
    @param value 
    @return
    */
   public boolean lSet(String keyList<Objectvalue) {
       try {
           redisTemplate.opsForList().rightPushAll(keyvalue);
           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * list缓存
    @param key 
    @param value 
    @param time 时间()
    * @return
    */
   public boolean lSet(String keyList<Objectvaluelong time) {
       try {
           redisTemplate.opsForList().rightPushAll(keyvalue);
           if (time 0) {
               expire(keytime);
           }
           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * 根据索引修改list中的某条数据
    @param key 
    @param index 索引
    @param value 
    @return
    */
   public boolean lUpdateIndex(String keylong index,Object value) {
       try {
           redisTemplate.opsForList().set(keyindexvalue);
           return true;
       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
   }
   /**
    * 移除N个值为value
    * @param key 
    @param count 移除多少个
    @param value 
    @return 移除的个数
    */
   public long lRemove(String key,long count,Object value) {
       try {
           Long remove redisTemplate.opsForList().remove(keycountvalue);
           return remove;
       } catch (Exception e) {
           e.printStackTrace();
           return 0;
       }
   }
}


 

明天计划完成的事情

明天计划完成Redis的使用和测试,并学习Memcache的使用和测试,学习JMeter的使用

学会缓存的使用,简单理解缓存的过程

 

 

遇到的问题

网上资料比较难找,规范的缓存工具类需要甄别,正确的部署流程也很少讲清楚

 

 

收获

学会了Redis的部署,找到了Redis的工具类,看B站教程对缓存的概念有一定理解



返回列表 返回列表
评论

    分享到