Shiro 集成Spring 使用 redis时 使用redisTemplate替代jedisPool(五)

1、添加依赖架包:

 1 <dependency>
 2                 <groupId>org.springframework.data</groupId>
 3                 <artifactId>spring-data-redis</artifactId>
 4                 <version>${spring-data-redis.version}</version>
 5             </dependency>
 6             <!-- 用于jedis-spring-data redisTemplate -->
 7             <dependency>
 8                 <groupId>org.apache.commons</groupId>
 9                 <artifactId>commons-pool2</artifactId>
10                 <version>${apache-commons-pool2}</version>
11             </dependency>

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

 1 <properties>
 2 <!-- 使用redisTamplate结合jedisPoolConifg 必须使用jedis版本2.9.0 -->
 3         <jedis.version>2.9.0</jedis.version>
 4 </properties>
 5 <!-- Redis客户端 -->
 6             <dependency>
 7                 <groupId>redis.clients</groupId>
 8                 <artifactId>jedis</artifactId>
 9                 <version>${jedis.version}</version>
10             </dependency>

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

 1 #single redis
 2 redis.single.client.host=192.168.180.42
 3 redis.single.client.port=6379
 4 redis.password=[email protected]
 5
 6 #最大分配的对象数
 7 redis.pool.maxActive=1024
 8 #最大能够保持idel状态的对象数
 9 redis.pool.maxIdle=200
10 #当池内没有返回对象时,最大等待时间
11 redis.pool.maxWait=1000
12 #当调用borrow Object方法时,是否进行有效性检查
13 redis.pool.testOnBorrow=true
14 #当调用return Object方法时,是否进行有效性检查
15 redis.pool.testOnReturn=true

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

 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:c="http://www.springframework.org/schema/c"
 6        xmlns:cache="http://www.springframework.org/schema/cache"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans
 8        http://www.springframework.org/schema/beans/spring-beans.xsd
 9        http://www.springframework.org/schema/cache
10        http://www.springframework.org/schema/cache/spring-cache.xsd">
11
12     <!-- 以前项目中的配置,注意需要添加Spring Data Redis等jar包 -->
13     <description>redis配置</description>
14
15     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
16         <property name="maxIdle" value="${redis.pool.maxIdle}"/>
17         <property name="maxTotal" value="${redis.pool.maxActive}"/>
18         <property name="maxWaitMillis" value="${redis.pool.maxWait}"/>
19         <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
20         <property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
21     </bean>
22
23     <!-- JedisConnectionFactory -->
24     <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
25         <property name="hostName" value="${redis.single.client.host}"/>
26         <property name="port" value="${redis.single.client.port}"/>
27         <property name="password" value="${redis.password}"></property>
28         <property name="poolConfig" ref="jedisPoolConfig"/>
29     </bean>
30
31     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
32           p:connectionFactory-ref="jedisConnectionFactory">
33         <!-- 若使用redis作为shiro的缓存,则开放此处,需要使用jdk序列化 -->
34         <!--<property name="keySerializer">-->
35         <!--<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>-->
36         <!--</property>-->
37         <!--<property name="valueSerializer">-->
38         <!--<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>-->
39         <!--</property>-->
40         <!--<property name="hashKeySerializer">-->
41         <!--<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>-->
42         <!--</property>-->
43         <!--<property name="hashValueSerializer">-->
44         <!--<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>-->
45         <!--</property>-->
46
47         <!-- 若不使用redis作为shiro的缓存,则开放此处,使用string序列化即可,jdk序列化在redis中的key值 不好看 -->
48         <property name="keySerializer">
49             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
50         </property>
51         <property name="valueSerializer">
52             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
53         </property>
54         <property name="hashKeySerializer">
55             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
56         </property>
57         <property name="hashValueSerializer">
58             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
59         </property>
60     </bean>
61
62     <!--spring cache-->
63     <!--<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"-->
64           <!--c:redisOperations-ref="redisTemplate">-->
65         <!--&lt;!&ndash;         默认缓存10分钟 &ndash;&gt;-->
66         <!--<property name="defaultExpiration" value="10"/>-->
67         <!--&lt;!&ndash; key:prefix &ndash;&gt;-->
68         <!--<property name="usePrefix" value="true"/>-->
69         <!--&lt;!&ndash;         cacheName 缓存超时配置,半小时,一小时,一天 &ndash;&gt;-->
70         <!--<property name="expires">-->
71             <!--<map key-type="java.lang.String" value-type="java.lang.Long">-->
72                 <!--<entry key="halfHour" value="1800"/>-->
73                 <!--<entry key="hour" value="3600"/>-->
74                 <!--<entry key="oneDay" value="86400"/>-->
75                 <!--<entry key="itzixiCaptcha" value="500"/>-->
76                 <!--&lt;!&ndash;                 shiro cache keys &ndash;&gt;-->
77                 <!--<entry key="authenticationCache" value="1800"/>&lt;!&ndash; 用户每次操作后会要等缓存过期后会重新再取 &ndash;&gt;-->
78                 <!--<entry key="authorizationCache" value="1800"/>&lt;!&ndash; 用户每次操作后会要等缓存过期后会重新再取 &ndash;&gt;-->
79                 <!--<entry key="activeSessionCache" value="1800"/>&lt;!&ndash; 用户session每次操作后会重置时间 &ndash;&gt;-->
80             <!--</map>-->
81         <!--</property>-->
82     <!--</bean>-->
83
84     <!-- cache注解,项目中如果还存在shiro的ehcache的话,那么本文件和spring-ehcache.xml中的只能使用一个 -->
85     <!--<cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/>-->
86
87 </beans>

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

RedisOperator

  1 import java.util.Map;
  2 import java.util.Set;
  3 import java.util.concurrent.TimeUnit;
  4
  5 import org.springframework.beans.factory.annotation.Autowired;
  6 import org.springframework.data.redis.core.RedisTemplate;
  7 import org.springframework.stereotype.Component;
  8
  9 @Component
 10 public class RedisOperator {
 11
 12     @Autowired
 13     private RedisTemplate<String, Object> redisTemplate;
 14
 15     // Key(键),简单的key-value操作
 16
 17     /**
 18      * 实现命令:TTL key,以秒为单位,返回给定 key的剩余生存时间(TTL, time to live)。
 19      *
 20      * @param key
 21      * @return
 22      */
 23     public long ttl(String key) {
 24         return redisTemplate.getExpire(key);
 25     }
 26
 27     /**
 28      * 实现命令:expire 设置过期时间,单位秒
 29      *
 30      * @param key
 31      * @return
 32      */
 33     public void expire(String key, long timeout) {
 34         redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
 35     }
 36
 37     /**
 38      * 实现命令:INCR key,增加key一次
 39      *
 40      * @param key
 41      * @return
 42      */
 43     public long incr(String key, long delta) {
 44         return redisTemplate.opsForValue().increment(key, delta);
 45     }
 46
 47     /**
 48      * 实现命令:KEYS pattern,查找所有符合给定模式 pattern的 key
 49      */
 50     public Set<String> keys(String pattern) {
 51         return redisTemplate.keys(pattern);
 52     }
 53
 54     /**
 55      * 实现命令:DEL key,删除一个key
 56      *
 57      * @param key
 58      */
 59     public void del(String key) {
 60         redisTemplate.delete(key);
 61     }
 62
 63     // String(字符串)
 64
 65     /**
 66      * 实现命令:SET key value,设置一个key-value(将字符串值 value关联到 key)
 67      *
 68      * @param key
 69      * @param value
 70      */
 71     public void set(String key, String value) {
 72         redisTemplate.opsForValue().set(key, value);
 73     }
 74
 75     /**
 76      * 实现命令:SET key value EX seconds,设置key-value和超时时间(秒)
 77      *
 78      * @param key
 79      * @param value
 80      * @param timeout
 81      *            (以秒为单位)
 82      */
 83     public void set(String key, String value, long timeout) {
 84         redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
 85     }
 86
 87     /**
 88      * 实现命令:GET key,返回 key所关联的字符串值。
 89      *
 90      * @param key
 91      * @return value
 92      */
 93     public String get(String key) {
 94         return (String)redisTemplate.opsForValue().get(key);
 95     }
 96
 97     // Hash(哈希表)
 98
 99     /**
100      * 实现命令:HSET key field value,将哈希表 key中的域 field的值设为 value
101      *
102      * @param key
103      * @param field
104      * @param value
105      */
106     public void hset(String key, String field, Object value) {
107         redisTemplate.opsForHash().put(key, field, value);
108     }
109
110     /**
111      * 实现命令:HGET key field,返回哈希表 key中给定域 field的值
112      *
113      * @param key
114      * @param field
115      * @return
116      */
117     public String hget(String key, String field) {
118         return (String) redisTemplate.opsForHash().get(key, field);
119     }
120
121     /**
122      * 实现命令:HDEL key field [field ...],删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。
123      *
124      * @param key
125      * @param fields
126      */
127     public void hdel(String key, Object... fields) {
128         redisTemplate.opsForHash().delete(key, fields);
129     }
130
131     /**
132      * 实现命令:HGETALL key,返回哈希表 key中,所有的域和值。
133      *
134      * @param key
135      * @return
136      */
137     public Map<Object, Object> hgetall(String key) {
138         return redisTemplate.opsForHash().entries(key);
139     }
140
141     // List(列表)
142
143     /**
144      * 实现命令:LPUSH key value,将一个值 value插入到列表 key的表头
145      *
146      * @param key
147      * @param value
148      * @return 执行 LPUSH命令后,列表的长度。
149      */
150     public long lpush(String key, String value) {
151         return redisTemplate.opsForList().leftPush(key, value);
152     }
153
154     /**
155      * 实现命令:LPOP key,移除并返回列表 key的头元素。
156      *
157      * @param key
158      * @return 列表key的头元素。
159      */
160     public String lpop(String key) {
161         return (String)redisTemplate.opsForList().leftPop(key);
162     }
163
164     /**
165      * 实现命令:RPUSH key value,将一个值 value插入到列表 key的表尾(最右边)。
166      *
167      * @param key
168      * @param value
169      * @return 执行 LPUSH命令后,列表的长度。
170      */
171     public long rpush(String key, String value) {
172         return redisTemplate.opsForList().rightPush(key, value);
173     }
174
175 }

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

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

7、如:

原文地址:https://www.cnblogs.com/yinfengjiujian/p/9084711.html

时间: 2024-08-09 16:35:13

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

spring 的redis操作类RedisTemplate

spring 集成的redis操作几乎都在RedisTemplate内了. 已spring boot为例, 再properties属性文件内配置好 redis的参数 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=redispass spring.redis.database=0 spring.redis.timeout=5000 再到 Application启动类下加入以下代码: @Bean pu

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

首先当然是项目中需要增加shiro的架包依赖: 1 <!-- shiro --> 2 <dependency> 3 <groupId>org.apache.shiro</groupId> 4 <artifactId>shiro-core</artifactId> 5 <version>${shiro.version}</version> 6 <exclusions> 7 <exclusion&

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

完整项目代码位于码云上,点击获取:Git地址 主要介绍一下重点配置地方: 一.application.yml文件 server: port: 8084 servlet: context-path: /testspring: ## quartz定时任务,采用数据库方式 quartz: job-store-type: jdbc #json 时间戳统一转换 jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 aop: proxy-tar

java版spring cloud+spring boot+redis社交电子商务平台-docker-feign配置(五)

简介 B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六.上一节我们讨论了怎么用feign声明式调用cloud的生产者,这节我们讨论一下feign配置,通过编写配置类,我们可以自定义feign的日志级别,日志扫描目录,可以通过feign调用服务在eureka上的调用信息. feign声明接口之后,在代码中通过@Resource或者@Autowired注入之后即可使用. @FeignClient标签的常用属性如下: name:指定FeignClient的名称,如果项目使用了Ribbon,

Spring MVC集成Spring Data Reids和Spring Session实现Session共享

说明:Spring MVC中集成Spring Data Redis和Spring Session时版本是一个坑点,比如最新版本的Spring Data Redis已经不包含Jedis了,需要自行引入.且最新版本的2.0.1会与Spring MVC 4.1.4有冲突,估计写法错了.所以要明确引入的Spring MVC版本和Spring Data Redis和Spring Session版本. 小提示:如果想要官方明确的版本可以参考Spring Boot的版本,比如我使用了1.4.7的Spring

Spring Data Redis简介以及项目Demo,RedisTemplate和 Serializer详解

一.概念简介: Redis: Redis是一款开源的Key-Value数据库,运行在内存中,由ANSI C编写,详细的信息在Redis官网上面有,因为我自己通过google等各种渠道去学习Redis,走了不少弯路,所以总结一条我认为不错的学习路径给大家: 1.<The Little Redis Book> 是一本开源PDF,只有29页的英文文档,看完后对Redis的基本概念应该差不多熟悉了,剩下的可以去Redis官网熟悉相关的命令. 2.<Redis设计与实现> 如果想继续深入,推

spring mvc Spring Data Redis RedisTemplate [转]

一.概念简介: Redis: Redis是一款开源的Key-Value数据库,运行在内存中,由ANSI C编写,详细的信息在Redis官网上面有,因为我自己通过google等各种渠道去学习Redis,走了不少弯路,所以总结一条我认为不错的学习路径给大家: 1.<The Little Redis Book> 是一本开源PDF,只有29页的英文文档,看完后对Redis的基本概念应该差不多熟悉了,剩下的可以去Redis官网熟悉相关的命令. 2.<Redis设计与实现> 如果想继续深入,推

shiro和springMVC和redis集成

pom.xml中 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-a

Spring boot后台搭建二为Shiro权限控制添加Redis缓存

在添加权限控制后,添加方法 查看 当用户访问”获取用户信息”.”新增用户”和”删除用户”的时,后台输出打印如下信息 , Druid数据源SQL监控 为了避免频繁访问数据库获取权限信息,在Shiro中加入缓存 缓存有基于Redis和Ehcache的,本文只介绍基于Redis的 1.Shiro集成Redis的引入依赖 <dependency> <groupId>org.crazycake</groupId> <artifactId>shiro-redis<