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



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM