Shiro教程(四)Shiro + Redis配置

我们知道Shiro 提供了一系列让我们自己实现的接口,包括org.apache.shiro.cache.CacheManagerorg.apache.shiro.cache.Cache  等接口。那么我们要对这些做实现,就实现了 Shiro  Session  和用户认证信息、用户缓存信息等的缓存,存储。我们可以用缓存,如 Redis  memcache  EHCache  等,甚至我们可以用数据库,如 Oracle  Mysql  等,都可以,只有效率的快慢问题,功能都可以达到, Redis  有一个更实用的功能,就是ttl功能,我们这时候又想到一个 Nosql  --> Elasticsearch  也是带ttl功能的,那么也可以用它来实现。

那么我的教程是采用了 Redis  ,而且是用了JedisJedis 可以实现poolhash 的集群Redis

在我的教程里命名为spring-cache.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:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  11. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
  12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
  13. default-lazy-init="false">
  14.  
  15. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  16. <property name="maxActive" value="500"/><!-- 最大连接数 -->
  17. <property name="maxIdle" value="100"/><!-- 最大闲置 -->
  18. <property name="minIdle" value="10"/><!-- 最小闲置 -->
  19. <property name="maxWait" value="5000"/><!-- 最大等待 -->
  20. <property name="testOnBorrow" value="true"/><!-- 可以获取 -->
  21. </bean>
  22.  
  23. <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
  24. <constructor-arg index="0" ref="jedisPoolConfig"/>
  25. <constructor-arg index="2" value="6379"/><!-- 端口 -->
  26. <constructor-arg index="3" value="5000"/><!-- 超时 -->
  27. <constructor-arg index="1" value="127.0.0.1"/><!--Redis IP地址 -->
  28. </bean>
  29.  
  30. </beans>

如果 Redis  设置了密码,需要这样配置:

 
  1. <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
  2. <constructor-arg index="0" ref="jedisPoolConfig"/>
  3. <constructor-arg index="2" value="6379"/><!-- 端口 -->
  4. <constructor-arg index="3" value="5000"/><!-- 超时 -->
  5. <constructor-arg index="1" value="127.0.0.1"/><!--Redis IP地址 -->
  6. <constructor-arg index="4" value="Redis-pswd"/><!--密码 -->
  7. </bean>

对应的构造方法:

 
  1.     public JedisPool(final Config poolConfig, final String host, int port,
  2. int timeout, final String password) {
  3. this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE);
  4. }

那么 Shiro  中的 Redis  配置已经配置好了,关于 Shiro  + Redis  配置请看下一篇博客,会详细讲到,原因是我觉得和下一篇和 Spring  的配置文件一起讲会关联性更好一点。

猜你喜欢

转载自blog.csdn.net/baidu_37366055/article/details/88072093