使用spring-data-redis操作redis


一、maven
<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.1.0</version>
        </dependency>
 

二、spring 配置:
添加spring格式頭
xmlns:p=http://www.springframework.org/schema/p

配置bean:
 <bean id="jedisConnFactory" 
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
        p:use-pool="true" p:host-name="192.168.0.60" p:port="6379"/>
    <!-- redis template definition -->
    <bean id="redisTemplate" 
        class="org.springframework.data.redis.core.RedisTemplate" 
        p:connection-factory-ref="jedisConnFactory">
        <!-- 使用string主要是key 在redis端用命令好讀 不然默認的序列化沒辦法讀 -->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
    </bean>  
 

三、例子
package com.vrv.im.service.impl.helper;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.vrv.im.domain.IMCommentSubject;

/** 
 * @date 2013年12月26日 上午11:47:22 
 * @version V1.0   
 * @Description: 緩存新鮮事的最近兩條評論,所有的新鮮事,
 * value如果默認序列化存儲佔用空間大,可以使用hashmap 速度快 佔用空間小 redis端可讀性強 
 * 默認的就是代碼簡潔
 * 
 * 緩存對象字段有點多,可以優化減少
 */
@Component
public class CommentInfoCacheHandler {
	 @Autowired
	 private RedisTemplate<String, String> template;
	  private String commentInfoKey="commentInfo";
	  // inject the template as ListOperations
	  @Resource(name="redisTemplate")
	  private ListOperations<String,IMCommentSubject> commentInfoHash;
	  /**
	   * 獲取緩存評論
	   * @param subjectCommentID
	   * @return
	   */
	  public List<IMCommentSubject> getCommentInfoCache(long subjectCommentID){
		  List<IMCommentSubject> list=  commentInfoHash.range(commentInfoKey+"_"+subjectCommentID, 0, -1);//獲取所有
		  if(list==null)
			 list= new ArrayList<IMCommentSubject>();
		  return list;
	  }
	  /**
	   * 添加緩存信息
	   * @param commentInfo
	   */
	  public void addCommentInfoCache(IMCommentSubject commentInfo){
		  commentInfoHash.leftPush(commentInfoKey+"_"+commentInfo.getSubjectCommentID(), commentInfo);
		  commentInfoHash.trim(commentInfoKey+"_"+commentInfo.getSubjectCommentID(), 0, 1);//保留2條
	  }
	  /**
	   * 刪除評論主體緩存
	   * @param subjectCommentID
	   */
	 public void deleteCommentInfoCache(long subjectCommentID){
		 template.delete(commentInfoKey+"_"+subjectCommentID);//所有都刪除
	 }
	 
	  /**
	   * 刪除評論主體的某條評論
	   * @param subjectCommentID
	   */
	 public void deleteCommentInfoCache(IMCommentSubject commentInfo){
		 commentInfoHash.remove(commentInfoKey+"_"+commentInfo.getSubjectCommentID(), 0, commentInfo);//相等的評論信息
	 } 
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章