使用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);//相等的评论信息
	 } 
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值