redisTemplate 操作
redisDao封装类-其他dao集成他
package com.ffcs.wlan.dao.common;
import javax.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate; /**
* AbstractBaseRedisDao
* @author hugsh
* @version <b>1.0</b>
*/
public abstract class AbstractBaseRedisDao<K, V> { @Resource
protected StringRedisTemplate redisTemplate; public void setRedisTemplate(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
}
批量插入(不关注返回值)
@Repository
public class RedisInitDao extends AbstractBaseRedisDao<String, Object> { Logger logger=Logger.getLogger(RedisInitDao.class); /**
* 批量向redis中插入H码:key(tableName:hcode) value(pcode)
* 如果键已存在则返回false,不更新,防止覆盖。使用pipeline批处理方式(不关注返回值)
* @param list 一个map代表一行记录,2个key:hcode & pcode。
* @param tableName redis中key的值为tableName:hcode 对应value值为pcode。
* @return
*/
public boolean addHcode(final List<Map<String, Object>> list,final String tableName) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
for (Map<String, Object> 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;
}
批量获取(有返回值)
/**
* 从redis中获取(获取密码日志) rPop从链表尾部弹出(最早的日志)
* 多线程并发读取日志长度的时候,比如都得到结果是1000条。
* 当多线程每个都 循环1000次 pop弹出 日志的时候,
* 由于是多线程一起pop,所以每个线程获得的数组中都会包含 null 甚至有的全是null
* @return
*/
public List<String> getLogFromRedis() { final RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
//密码日志的长度
final Long pwdLogSize=redisTemplate.opsForList().size("getpwdList"); List<Object> pwdLogList=redisTemplate.executePipelined(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection conn)
throws DataAccessException {
for (int i= ;i<pwdLogSize ;i++) {
byte[] listName = serializer.serialize("getpwdList");
conn.rPop(listName);
}
return null;
}
}, serializer); // 去除结果中的null
ArrayList<String> newList=new ArrayList<String>();
for (Object o : pwdLogList) {
if(o!=null)
newList.add(String.valueOf(o));
}
return newList;
}
基础数据类型工具类(opsForList)
/**
* 向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());
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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"> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"></property>
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean> </beans>
<!-- 引入项目配置文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:redis.properties</value><!-- 引入redis配置文件 -->
<value>classpath:jdbc.properties</value><!-- 定义spring-jdbc配置信息路径 -->
</list>
</property>
</bean> <!-- 自动扫描model,dao和service包(自动注入) -->
<context:component-scan base-package="com.ffcs.wlan.model,com.ffcs.wlan.dao,com.ffcs.wlan.service" />
redisTemplate 操作的更多相关文章
- Spring RedisTemplate操作-xml配置(1)
网上没能找到全的spring redistemplate操作例子,故特意化了点时间做了接口调用练习,基本包含了所有redistemplate方法. 该操作例子是个系列,该片为spring xml配置, ...
- redis命令和RedisTemplate操作对应表
redis命令和RedisTemplate操作对应表 redisTemplate.opsForValue();//操作字符串 redisTemplate.opsForHash();//操作hash r ...
- spring data redis RedisTemplate操作redis相关用法
http://blog.mkfree.com/posts/515835d1975a30cc561dc35d spring-data-redis API:http://docs.spring.io/sp ...
- 在Java中使用redisTemplate操作缓存
背景 在最近的项目中,有一个需求是对一个很大的数据库进行查询,数据量大概在几千万条.但同时对查询速度的要求也比较高. 这个数据库之前在没有使用Presto的情况下,使用的是Hive,使用Hive进行一 ...
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
RedisTemplate如何检查一个key是否存在? return getRedisTemplate().hasKey(key); 由一个问题,复习了一下redis 抄自: https://www. ...
- SpringBoot 使用RedisTemplate操作Redis
新版: import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.T ...
- spring-data-redis 中使用RedisTemplate操作Redis
Redis 数据结构简介 Redis可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集合 ...
- RedisTemplate操作Redis
RedisTemplate Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序 ...
- Java 使用Jedis和RedisTemplate操作Redis缓存(SpringBoot)
package com.example.redis.controller; import com.example.redis.entity.User; import com.example.redis ...
- springboot中,使用redisTemplate操作redis
知识点: springboot中整合redis springboot中redisTemplate的使用 redis存数据时,key出现乱码问题 一:springboot中整合redis (1)pom. ...
随机推荐
- vagrant 入门3
创建第一个Vagrant虚拟环境以及工程(续集): (8) Provisioning: 通常情况下Box只做最基本的设置,而不是一次到位的设置好所有的环境.Vagrant通常使用chef或者Puppe ...
- orcad10.5启动加速
OrCAD不知道怎么滴,启动速度慢的让人崩溃!30秒算快的,有时候甚至几分钟,要么需要连续开N次才打开,苦啊!!经过网上无数文章的洗礼,无数次的尝试,终于得出一种特效方法! 原因:OrCAD启动时在电 ...
- 用macports装了一份openssl
我已经用macports装了一份openssl,然后自己又编译了一份openssl....第三方给Mac出的一个类似BSD Ports的一个软件包管理工具装的话只需要sudo port install ...
- perl学习(1) 入门
Perl 被设计成90%擅长处理文本,10%处理其余的问题.因此Perl 有强大的文本处理能力,包括正则表达式. 第一个程序 hello world #! /usr/bin/perl -w use s ...
- HDU 4734 F(x) 2013 ACM/ICPC 成都网络赛
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4734 数位DP. 用dp[i][j][k] 表示第i位用j时f(x)=k的时候的个数,然后需要预处理下小 ...
- Unity Notes调制粒子系统的颗粒的最大数目
Unity该粒子系统是很容易使用.这样的问题是在实际的过程中遇到的:以控制的粒子系统组件的动态需要可产生颗粒的最大数目. 看doc他说,有maxParticles控制.却没有这个开放的參数.仅仅能通过 ...
- C# 中datagridview行里面有三个cheeckbox,要控制成三选一。
我之前有试过在cellendedit中处理,可以达成效果,当不符合用户打单的界面要求.该事件是在单元格编辑结束之后, 当用户选中两个checkbox,且焦点不移开时,界面上会出现有两个checkbox ...
- 使用SourceTree Push 出现 POST git-receive-pack (chunked) 的解决方法
在使用SourceTree上传资料的时候,遇到 POST git-receive-pack (chunked) 从 stackoverflow 看到这样一则 This is a bug in Git; ...
- m序列产生器
扩频通讯中会用的到伪随机数列 —— m序列 代码中是5bit原始码 module m_generate ( clock , reset , m_code ); input clock ,reset ; ...
- 期权put和call
今天和朋友谈到了FB的期权操作问题,保证一个固定收益或者得到一个以低价买入FB的机会. 首先说下基本概念:call:是指买权put :是指卖权但是这两种期权又分别对应了long和short的两种操作, ...