1、添加依赖架包:

 <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring-data-redis.version}</version>
</dependency>
<!-- 用于jedis-spring-data redisTemplate -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>${apache-commons-pool2}</version>
</dependency>

2、使用的 jedis 必须是2.9.0以后的版本

 <properties>
<!-- 使用redisTamplate结合jedisPoolConifg 必须使用jedis版本2.9.0 -->
<jedis.version>2.9.0</jedis.version>
</properties>
<!-- Redis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>

3、在resource下新建立redis的属性properties文件 redis.properties

 #single redis
redis.single.client.host=192.168.180.42
redis.single.client.port=6379
redis.password=1qaz@WSX #最大分配的对象数
redis.pool.maxActive=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true

4、增加redis的配置xml文件 applicationContext-redis.xml

 <?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:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- 以前项目中的配置,注意需要添加Spring Data Redis等jar包 -->
<description>redis配置</description> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.pool.maxIdle}"/>
<property name="maxTotal" value="${redis.pool.maxActive}"/>
<property name="maxWaitMillis" value="${redis.pool.maxWait}"/>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
<property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
</bean> <!-- JedisConnectionFactory -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.single.client.host}"/>
<property name="port" value="${redis.single.client.port}"/>
<property name="password" value="${redis.password}"></property>
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connectionFactory-ref="jedisConnectionFactory">
<!-- 若使用redis作为shiro的缓存,则开放此处,需要使用jdk序列化 -->
<!--<property name="keySerializer">-->
<!--<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>-->
<!--</property>-->
<!--<property name="valueSerializer">-->
<!--<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>-->
<!--</property>-->
<!--<property name="hashKeySerializer">-->
<!--<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>-->
<!--</property>-->
<!--<property name="hashValueSerializer">-->
<!--<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>-->
<!--</property>--> <!-- 若不使用redis作为shiro的缓存,则开放此处,使用string序列化即可,jdk序列化在redis中的key值 不好看 -->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
</bean> <!--spring cache-->
<!--<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"-->
<!--c:redisOperations-ref="redisTemplate">-->
<!--&lt;!&ndash; 默认缓存10分钟 &ndash;&gt;-->
<!--<property name="defaultExpiration" value="10"/>-->
<!--&lt;!&ndash; key:prefix &ndash;&gt;-->
<!--<property name="usePrefix" value="true"/>-->
<!--&lt;!&ndash; cacheName 缓存超时配置,半小时,一小时,一天 &ndash;&gt;-->
<!--<property name="expires">-->
<!--<map key-type="java.lang.String" value-type="java.lang.Long">-->
<!--<entry key="halfHour" value="1800"/>-->
<!--<entry key="hour" value="3600"/>-->
<!--<entry key="oneDay" value="86400"/>-->
<!--<entry key="itzixiCaptcha" value="500"/>-->
<!--&lt;!&ndash; shiro cache keys &ndash;&gt;-->
<!--<entry key="authenticationCache" value="1800"/>&lt;!&ndash; 用户每次操作后会要等缓存过期后会重新再取 &ndash;&gt;-->
<!--<entry key="authorizationCache" value="1800"/>&lt;!&ndash; 用户每次操作后会要等缓存过期后会重新再取 &ndash;&gt;-->
<!--<entry key="activeSessionCache" value="1800"/>&lt;!&ndash; 用户session每次操作后会重置时间 &ndash;&gt;-->
<!--</map>-->
<!--</property>-->
<!--</bean>--> <!-- cache注解,项目中如果还存在shiro的ehcache的话,那么本文件和spring-ehcache.xml中的只能使用一个 -->
<!--<cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/>--> </beans>

5、使用redisTemplate的操作实现类 编写

RedisOperator

 import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component; @Component
public class RedisOperator { @Autowired
private RedisTemplate<String, Object> redisTemplate; // Key(键),简单的key-value操作 /**
* 实现命令:TTL key,以秒为单位,返回给定 key的剩余生存时间(TTL, time to live)。
*
* @param key
* @return
*/
public long ttl(String key) {
return redisTemplate.getExpire(key);
} /**
* 实现命令:expire 设置过期时间,单位秒
*
* @param key
* @return
*/
public void expire(String key, long timeout) {
redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
} /**
* 实现命令:INCR key,增加key一次
*
* @param key
* @return
*/
public long incr(String key, long delta) {
return redisTemplate.opsForValue().increment(key, delta);
} /**
* 实现命令:KEYS pattern,查找所有符合给定模式 pattern的 key
*/
public Set<String> keys(String pattern) {
return redisTemplate.keys(pattern);
} /**
* 实现命令:DEL key,删除一个key
*
* @param key
*/
public void del(String key) {
redisTemplate.delete(key);
} // String(字符串) /**
* 实现命令:SET key value,设置一个key-value(将字符串值 value关联到 key)
*
* @param key
* @param value
*/
public void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
} /**
* 实现命令:SET key value EX seconds,设置key-value和超时时间(秒)
*
* @param key
* @param value
* @param timeout
* (以秒为单位)
*/
public void set(String key, String value, long timeout) {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
} /**
* 实现命令:GET key,返回 key所关联的字符串值。
*
* @param key
* @return value
*/
public String get(String key) {
return (String)redisTemplate.opsForValue().get(key);
} // Hash(哈希表) /**
* 实现命令:HSET key field value,将哈希表 key中的域 field的值设为 value
*
* @param key
* @param field
* @param value
*/
public void hset(String key, String field, Object value) {
redisTemplate.opsForHash().put(key, field, value);
} /**
* 实现命令:HGET key field,返回哈希表 key中给定域 field的值
*
* @param key
* @param field
* @return
*/
public String hget(String key, String field) {
return (String) redisTemplate.opsForHash().get(key, field);
} /**
* 实现命令:HDEL key field [field ...],删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。
*
* @param key
* @param fields
*/
public void hdel(String key, Object... fields) {
redisTemplate.opsForHash().delete(key, fields);
} /**
* 实现命令:HGETALL key,返回哈希表 key中,所有的域和值。
*
* @param key
* @return
*/
public Map<Object, Object> hgetall(String key) {
return redisTemplate.opsForHash().entries(key);
} // List(列表) /**
* 实现命令:LPUSH key value,将一个值 value插入到列表 key的表头
*
* @param key
* @param value
* @return 执行 LPUSH命令后,列表的长度。
*/
public long lpush(String key, String value) {
return redisTemplate.opsForList().leftPush(key, value);
} /**
* 实现命令:LPOP key,移除并返回列表 key的头元素。
*
* @param key
* @return 列表key的头元素。
*/
public String lpop(String key) {
return (String)redisTemplate.opsForList().leftPop(key);
} /**
* 实现命令:RPUSH key value,将一个值 value插入到列表 key的表尾(最右边)。
*
* @param key
* @param value
* @return 执行 LPUSH命令后,列表的长度。
*/
public long rpush(String key, String value) {
return redisTemplate.opsForList().rightPush(key, value);
} }

6、申明为@Component 的组件  那么需要在 spring mvc配置文件、spring service文件中增加扫描这个包的 配置

那么在需要使用redis的地方 可以注入使用

7、如:

Shiro 集成Spring 使用 redis时 使用redisTemplate替代jedisPool(五)的更多相关文章

  1. shiro 集成spring 使用 redis作为缓存 学习记录(六)

    1.在applicationContext-redis.xml配置文件中增加如下: 申明一个cacheManager对象 用来注入到  shiro的   securityManager 属性  cac ...

  2. 使用Spring Data Redis时,遇到的几个问题

    需求: 1,保存一个key-value形式的结构到redis 2,把一个对象保存成hash形式的结构到redis 代码如下: // 保存key-value值         pushFrequency ...

  3. shiro集成spring&工作流程&DelegatingFilterProxy

    1.集成Spring 参考文献: 新建web工程: ehcache-core来自Hibernate wen.xml <?xml version="1.0" encoding= ...

  4. Redis的安装以及spring整合Redis时出现Could not get a resource from the pool

    Redis的下载与安装 在Linux上使用wget http://download.redis.io/releases/redis-5.0.0.tar.gz下载源码到指定位置 解压:tar -xvf ...

  5. Shiro集成Spring

    本篇博客主要讲述的是两者的集成.不涉及到各自的详细细节和功能. 因为官方给出的文档不够具体,对新手而言通过官方文档还不可以非常快的搭建出SpringShiro的webproject.本博客将通过实际的 ...

  6. shiro 集成spring 配置 学习记录(一)

    首先当然是项目中需要增加shiro的架包依赖: <!-- shiro --> <dependency> <groupId>org.apache.shiro</ ...

  7. spring 的redis操作类RedisTemplate

    spring 集成的redis操作几乎都在RedisTemplate内了. 已spring boot为例, 再properties属性文件内配置好 redis的参数 spring.redis.host ...

  8. springboot集成shiro集成mybatis-plus、redis、quartz定时任务

    完整项目代码位于码云上,点击获取:Git地址 主要介绍一下重点配置地方: 一.application.yml文件 server: port: 8084 servlet: context-path: / ...

  9. shiro学习(四、shiro集成spring+springmvc)

    依赖:spring-context,spring-MVC,shiro-core,shiro-spring,shiro-web 实话实说:web.xml,spring,springmvc配置文件好难 大 ...

随机推荐

  1. webservice-之使用axis+spring开发

    一.环境配置 :在 eclipse 中配置引入相应的 spring框架( core/Remoting/Web ). axis 包.   二.代码开发 1.  在 MyEclipse 中建立一个新的 J ...

  2. CentOS(五)--Linux系统的分区概念

    一.系统分区 首先先普及一下有关系统分区的一些知识: 硬盘分区有三种,主磁盘分区.扩展磁盘分区.逻辑分区. 一个硬盘主分区至少有1个,最多4个,扩展分区可以没有,最多1个.且主分区+扩展分区总共不能超 ...

  3. 历届试题 小数第n位(小技巧)

    问题描述 我们知道,整数做除法时,有时得到有限小数,有时得到无限循环小数. 如果我们把有限小数的末尾加上无限多个0,它们就有了统一的形式. 本题的任务是:在上面的约定下,求整数除法小数点后的第n位开始 ...

  4. rsync之脑袋疼

    rsync图片参考 d本地模式,cp的感觉 vzrtopg = a - d -l --delete适用于2个目录完全一样的情况 默认avz就可以了 2,远端的shell 解决ssh链接慢的问题 3.d ...

  5. django随机验证码

    Python生成随机验证码,需要使用PIL模块. 安装: 1 python3.5 -m pip install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 from P ...

  6. http 和 https 区别?

    1. HTTP 的URL 以http:// 开头,而HTTPS 的URL 以https:// 开头2. HTTP 是不安全的,而 HTTPS 是安全的3. HTTP 标准端口是80 ,而 HTTPS ...

  7. 浅谈PHP面向对象编程(七、抽象类与接口)

    7.0 抽象类与接口 当定义一个类时,常常需要定义一些方法来描述该类的行为特征.但有时这些方法的实现方式是无法确定的,此时就可以使用抽象类和接口. 抽象类和接口用于提高程序的灵活性.抽象类是一种特殊的 ...

  8. MySql——编程

    基本语法形式 语句块模式: 在mysql编程中,begin....end;基本代替了原来编程语句中的{...}语法. 但又有所区别: 一个bigin...end;块,可以给定一个“标识符”,并且可以使 ...

  9. poj2356 Find a multiple

    /* POJ-2356 Find a multiple ----抽屉原理 Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total S ...

  10. [Cpp primer] Namespace using Declarations

    If we want to use cin in the standard library, we need to std::cin To simplify this work, we can do ...