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. bzoj1089严格n元树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1089 这是一种套路:记录“深度为 i ”的话,转移需要讨论许多情况:所以可以记录成“深度&l ...

  2. java.lang.NoSuchFieldError: TRACE

    Exception in thread "main" java.lang.NoSuchFieldError: TRACE    at org.jboss.logging.Log4j ...

  3. win7怎么安装和启动 jboss

    本文以JBoss Application Server 4.2.1 GA(以下简称JBoss)为例,介绍它在Windows平台上的启动过程.为了方便叙述,对平台环境做以下假定:Java运行时的安装路径 ...

  4. kotlin与fastjson的异常

    出现这个原因是因为kotlin的非空特性. 如果一个类中声明了一个字段(kotlin的特性,该字段默认是非空的), 使用fastjson进行转化的时候,如果json数据中没有该字段的数据,则会出现转换 ...

  5. Python控制台输出不换行(进度条等)

    sys.stdout.write('\r'+str) sys.stdout.flush() time.sleep(1)

  6. linux mount / umount 命令的基本用法 及 开机自动挂载

    格式:mount [-参数] [设备名称] [挂载点] 其中常用的参数有: -a 安装在/etc/fstab文件中类出的所有文件系统. -f 伪装mount,作出检查设备和目录的样子,但并不真正挂载文 ...

  7. GOF23设计模式之访问者模式(visitor)

    一.访问者模式概述 (1)模式动机   对于存储在一个集合中的对象,他们可能具有不同的类型(即使有一个公共的接口),对于该集合中的对象,可以接受一类称为访问者的对象来访问,不同的访问者其访问方式也有所 ...

  8. java代码---实现随机产生1000个随机数,并10个一行的输出

    总结:不会用,就是不熟 package com.s.x; //输入10个随机数,并显示最大值,最小值 import java.util.*; public class Value { public s ...

  9. python cx_Oracle模块的安装和使用

      $wget http://download.oracle.com/otn/linux/instantclient/10204/basic-10.2.0.4.0-linux-x86_64.zip 3 ...

  10. jQuery样式与动画

    修改内联CSS .css() 获取 //取得单个属性的值,传入'属性名',返回"value" .css('property') //取得多个属性的值,传入'['属性1','属性2' ...