redisTemplate 操作及相关配置

redisDao封装类-其他dao集成他

  1. package com.ffcs.wlan.dao.common;
  2. import javax.annotation.Resource;
  3. import org.springframework.data.redis.core.StringRedisTemplate;
  4.  
  5. /**
  6. * AbstractBaseRedisDao
  7. * @author hugsh
  8. * @version <b>1.0</b>
  9. */
  10. public abstract class AbstractBaseRedisDao<K, V> {
  11.  
  12. @Resource
  13. protected StringRedisTemplate redisTemplate;
  14.  
  15. public void setRedisTemplate(StringRedisTemplate redisTemplate) {
  16. this.redisTemplate = redisTemplate;
  17. }
  18. }

 批量插入(不关注返回值)

  1. @Repository
  2. public class RedisInitDao extends AbstractBaseRedisDao<String, Object> {
  3.  
  4. Logger logger=Logger.getLogger(RedisInitDao.class);
  5.  
  6. /**
  7. * 批量向redis中插入H码:key(tableName:hcode) value(pcode)
  8. * 如果键已存在则返回false,不更新,防止覆盖。使用pipeline批处理方式(不关注返回值)
  9. * @param list 一个map代表一行记录,2个key:hcode & pcode。
  10. * @param tableName redis中key的值为tableName:hcode 对应value值为pcode。
  11. * @return
  12. */
  13. public boolean addHcode(final List<Map<String, Object>> list,final String tableName) {
  14. boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
  15. public Boolean doInRedis(RedisConnection connection)
  16. throws DataAccessException {
  17. RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
  18. for (Map<String, Object> map : list) {
  19. byte[] key = serializer.serialize(tableName+":"+map.get("hcode").toString());
  20. byte[] name = serializer.serialize(map.get("pcode").toString());
  21. connection.setNX(key, name);
  22. }
  23. return true;
  24. }
  25. }, false, true);
  26. return result;
  27. }

批量获取(有返回值)

  1. /**
  2. * 从redis中获取(获取密码日志) rPop从链表尾部弹出(最早的日志)
  3. * 多线程并发读取日志长度的时候,比如都得到结果是1000条。
  4. * 当多线程每个都 循环1000次 pop弹出 日志的时候,
  5. * 由于是多线程一起pop,所以每个线程获得的数组中都会包含 null 甚至有的全是null
  6. * @return
  7. */
  8. public List<String> getLogFromRedis() {
  9.  
  10. final RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
  11. //密码日志的长度
  12. final Long pwdLogSize=redisTemplate.opsForList().size("getpwdList");
  13.  
  14. List<Object> pwdLogList=redisTemplate.executePipelined(new RedisCallback<String>() {
  15. @Override
  16. public String doInRedis(RedisConnection conn)
  17. throws DataAccessException {
  18. for (int i=0 ;i<pwdLogSize ;i++) {
  19. byte[] listName = serializer.serialize("getpwdList");
  20. conn.rPop(listName);
  21. }
  22. return null;
  23. }
  24. }, serializer);
  25.  
  26. // 去除结果中的null
  27. ArrayList<String> newList=new ArrayList<String>();
  28. for (Object o : pwdLogList) {
  29. if(o!=null)
  30. newList.add(String.valueOf(o));
  31. }
  32. return newList;
  33. }

基础数据类型工具类(opsForList)

  1. /**
  2. * 向redis中插入获取密码日志:leftPush 从链表头部压入
  3. * @param pwdLog 获取密码的日志
  4. * @return
  5. */
  6. public void addLogIntoRedis(final String pwdLog) {
  7. log.info("insert getpwd log into redis:"+pwdLog);
  8. try {
  9. redisTemplate.opsForList().leftPush("getpwdList", pwdLog);
  10. } catch (Exception e) {
  11. log.error(e.getMessage());
  12. }
  13. }

配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  11.  
  12.  
  13. <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  14. <property name="maxTotal" value="${redis.maxTotal}"></property>
  15. <property name="maxIdle" value="${redis.maxIdle}" />
  16. <property name="maxWaitMillis" value="${redis.maxWait}" />
  17. <property name="testOnBorrow" value="${redis.testOnBorrow}" />
  18. </bean>
  19.  
  20.  
  21. <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  22. p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig"/>
  23.  
  24. <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
  25. <property name="connectionFactory" ref="connectionFactory" />
  26. </bean>
  27.  
  28. </beans>
  1. <!-- 引入项目配置文件 -->
  2. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  3. <property name="locations">
  4. <list>
  5. <value>classpath:redis.properties</value><!-- 引入redis配置文件 -->
  6. <value>classpath:jdbc.properties</value><!-- 定义spring-jdbc配置信息路径 -->
  7. </list>
  8. </property>
  9. </bean>
  10.  
  11.  
  12. <!-- 自动扫描model,dao和service包(自动注入) -->
  13. <context:component-scan base-package="com.ffcs.wlan.model,com.ffcs.wlan.dao,com.ffcs.wlan.service" />

属性文件

    1. # Redis settings
    2. redis.host=192.168.11.100
    3. redis.port=6379
    4. #redis.pass=hugsh
    5. redis.maxIdle=25
    6. redis.maxTotal=250
    7. #redis.maxActive=600 invalid in2.4
    8. redis.maxWait=1000


原文地址:https://www.cnblogs.com/zhuyeshen/p/12147951.html