redis的客户端实现、主要分为spring-redis-data 、jredis。
记录下spring-redis-data的学习心得;
spring-redis-data 中我目前主要用了它的存、取、清除。
redis配置redis-manager-config.properties :
 
spring 中配置
 
<bean id="propertyConfigurerRedis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:config/redis-manager-config.properties</value>
            </list>
        </property>
    </bean>
        <!-- jedis pool配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxActive" value="${redis.maxActive}" />
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxWait" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>
    <!-- spring data redis -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="usePool" value="true"></property>
        <property name="hostName" value="${redis.host}" />
        <property name="port" value="${redis.port}" />
        <property name="password" value="${redis.pass}" />
        <property name="timeout" value="${redis.timeout}" />
        <property name="database" value="${redis.default.db}"></property>
        <constructor-arg index="0" ref="jedisPoolConfig" />
    </bean>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
    </bean>
<!--配置一个基础类(之后的业务类继承于该类)、将redisTemplate注入 -->
<bean id="redisBase" abstract="true">
  <property name="template" ref="redisTemplate"></property>
 </bean>
java代码:
 
public class RedisBase {
    private StringRedisTemplate template;
    /**
     * @return the template
     */
    public StringRedisTemplate getTemplate() {
        return template;
    }
    /**
     * @param template the template to set
     */
    public void setTemplate(StringRedisTemplate template) {
        this.template = template;
    }
}
 
继续:
具体redis的值的写入、读出、清除缓存!
第一:写入
 
public class StudentCountDO {
      private Long id;
         private String studentId;
          private Long commentHeadCount;
         private Long docAttitudeScores;
         private Long guideServiceScores;
          private Long treatEffectCount;
         private Long treatEffectScores;
      private String gmtModified;
      private String gmtCreated;
          private Long waitingTimeScores;
     }
StringRedisTemplate template = getTemplate();//获得上面注入的template
       // save as hash 一般key都要加一个前缀,方便清除所有的这类key
       BoundHashOperations<String, String, String> ops = template.boundHashOps("student:"+studentCount.getStudentId());
       Map<String, String> data = new HashMap<String, String>();
       data.put("studentId", CommentUtils.convertNull(studentCount.getStudentId()));
       data.put("commentHeadCount", CommentUtils.convertLongToString(studentCount.getCommentHeadCount()));
       data.put("docAttitudeScores", CommentUtils.convertLongToString(studentCount.getDocAttitudeScores()));
       data.put("guideServicesScores", CommentUtils.convertLongToString(studentCount.getGuideServiceScores()));
       data.put("treatEffectCount", CommentUtils.convertLongToString(studentCount.getTreatEffectCount()));
       data.put("treatEffectScores", CommentUtils.convertLongToString(studentCount.getTreatEffectScores()));
       data.put("waitingTimeScores", CommentUtils.convertLongToString(studentCount.getWaitingTimeScores()));
       try {
           ops.putAll(data);
       } catch (Exception e) {
           logger.error(CommentConstants.WRITE_EXPERT_COMMENT_COUNT_REDIS_ERROR + studentCount.studentCount(), e);
       }
 
第二、 取出
 
这个存和取的过程其实是把对象中的各个字段序列化之后存入到hashmap 、取出来的时候在进行按照存入进去的顺序进行取出。
第三 清除
这个就根据前面的前缀很简单了,一句代码就搞定啦!
 
pattern传入为student: 即可将该类型的所有缓存清除掉!