zoukankan      html  css  js  c++  java
  • 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=1qaz@WSX
     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、如:

  • 相关阅读:
    MIKROTIK ROS+PHP+MYSQL实现从数据库中配置DNS服务器
    随手记
    05 通过python开启静态http服务
    名称空间和作用域
    cs常用功能
    初识Cobalt Strike
    msf之手机木马生成&利用
    msf之meterpreter命令
    BURPSUITE的常用模块
    BURPSUITE专业汉化版安装
  • 原文地址:https://www.cnblogs.com/yinfengjiujian/p/9084711.html
Copyright © 2011-2022 走看看