template.java_RedisTemplate在java哪个包下面

展开全部

redisDao封装类-其他dao集成他

[java] view plain copy

package com.ffcs.wlan.dao.common;

import javax.annotation.Resource;

import org.springframework.data.redis.core.StringRedisTemplate;

/**

* AbstractBaseRedisDao

* @author hugsh

* @version 1.0

*/

public abstract class AbstractBaseRedisDao {

@Resource

protected StringRedisTemplate redisTemplate;

public void setRedisTemplate(StringRedisTemplate redisTemplate) {

this.redisTemplate = redisTemplate;

}

}

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

[java] view plain copy

@Repository

public class RedisInitDao extends AbstractBaseRedisDao {

Logger logger=Logger.getLogger(RedisInitDao.class);

/**

* 批量向redis中插入H码:key(tableName:hcode) value(pcode)

* 如果键已存在则返回false,不更新e5a48de588b662616964757a686964616f31333361303133,防止覆盖。使用pipeline批处理方式(不关注返回值)

* @param list 一个map代表一行记录,2个key:hcode & pcode。

* @param tableName redis中key的值为tableName:hcode 对应value值为pcode。

* @return

*/

public boolean addHcode(final List> list,final String tableName) {

boolean result = redisTemplate.execute(new RedisCallback() {

public Boolean doInRedis(RedisConnection connection)

throws DataAccessException {

RedisSerializer serializer = redisTemplate.getStringSerializer();

for (Map map : list) {

byte[] key = serializer.serialize(tableName+":"+map.get("hcode").toString());

byte[] name = serializer.serialize(map.get("pcode").toString());

connection.setNX(key, name);

}

return true;

}

}, false, true);

return result;

}

批量获取(有返回值)

[java] view plain copy

/**

* 从redis中获取(获取密码日志) rPop从链表尾部弹出(最早的日志)

* 多线程并发读取日志长度的时候,比如都得到结果是1000条。

* 当多线程每个都 循环1000次 pop弹出 日志的时候,

* 由于是多线程一起pop,所以每个线程获得的数组中都会包含 null 甚至有的全是null

* @return

*/

public List getLogFromRedis() {

final RedisSerializer serializer = redisTemplate.getStringSerializer();

//密码日志的长度

final Long pwdLogSize=redisTemplate.opsForList().size("getpwdList");

List pwdLogList=redisTemplate.executePipelined(new RedisCallback() {

@Override

public String doInRedis(RedisConnection conn)

throws DataAccessException {

for (int i=0 ;i

byte[] listName = serializer.serialize("getpwdList");

conn.rPop(listName);

}

return null;

}

}, serializer);

// 去除结果中的null

ArrayList newList=new ArrayList();

for (Object o : pwdLogList) {

if(o!=null)

newList.add(String.valueOf(o));

}

return newList;

}

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

[java] view plain copy

/**

* 向redis中插入获取密码日志:leftPush 从链表头部压入

* @param pwdLog 获取密码的日志

* @return

*/

public void addLogIntoRedis(final String pwdLog) {

log.info("insert getpwd log into redis:"+pwdLog);

try {

redisTemplate.opsForList().leftPush("getpwdList", pwdLog);

} catch (Exception e) {

log.error(e.getMessage());

}

}

配置文件

[html] view plain copy

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig"/>

[html] view plain copy

classpath:redis.properties

classpath:jdbc.properties

属性文件

[html] view plain copy

# Redis settings

redis.host=192.168.11.100

redis.port=6379

#redis.pass=hugsh

redis.maxIdle=25

redis.maxTotal=250

#redis.maxActive=600 invalid in2.4

redis.maxWait=1000

redis.testOnBorrow=true

本回答被网友采纳

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值