Spring-data-redis在shiro中的实例
来源:互联网 发布:黑马校对软件多少钱 编辑:程序博客网 时间:2024/06/11 19:38
简介
Redis是基于K-V的NoSql数据库,其主要将数据保存在内存中,极大的提高了存取速度。Redis常用作缓存、排名、投票、消息队列等。这里我主要介绍一下在shiro中,用Redis保存会话session,实现分布式会话的功能。
spring-data-redis是对Jedis的封装,方便redis的使用。
Redis安装和配置
首先安装Redis,为了方便,我直接在虚拟机VMWare安装,使用的系统为Ubuntu14.04.3。安装命令sudo apt-get install redis-server,安装成功后会自动启动Redis。运行redis-cli命令,检查Redis是否连接成功。修改Redis配置,否则远端不能连接上虚拟机中的Redis,参考http://blog.csdn.net/csujiangyu/article/details/49278801。
Redis在Spring中的配置
配置RedisTemplate
<context:property-placeholder location="classpath:property/redis.properties" /> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="usePool" value="true"/> <property name="hostName" value="${redis.host}" /> <property name="port" value="${redis.port}" /> <constructor-arg index="0" ref="jedisPoolConfig" /> </bean> <!-- redis template definition --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> </bean>
redis.properties配置Redis
redis.host=10.10.110.189redis.port=6379redis.pass=redis.maxIdle=10redis.maxTotal=10redis.testOnBorrow=true
Redis的使用
通过RedisTemplate操作Redis,实现增删查改的功能。
public class RedisCacheSessionDao extends CachingSessionDAO { @Inject private RedisTemplate<Serializable, Session> redisTemplate; @Resource(name = "redisTemplate") private ValueOperations<Serializable, Session> valueOps; @Override protected void doUpdate(Session session) { valueOps.set(session.getId(), session); } @Override protected void doDelete(Session session) { if (session == null || session.getId() == null) { return; } redisTemplate.delete(session.getId()); } @Override protected Serializable doCreate(Session session) { Serializable sessionId = generateSessionId(session); assignSessionId(session, sessionId); valueOps.set(sessionId, session); return sessionId; } @Override protected Session doReadSession(Serializable sessionId) { return valueOps.get(sessionId); }}
测试
从截图可以看出客户端的cookie中会话ID是1de10edf-badd-410a-928b-04a9346fa131
Redis中的会话ID是1de10edf-badd-410a-928b-04a9346fa131,不难得出sessionId已经插入Redis中。
源码
托管在https://github.com/Jdoing/myweb/tree/feature/distributed_session
1 0
- Spring-data-redis在shiro中的实例
- Spring-data-redis: serializer实例
- shiro 在spring中的配置
- Spring-data-redis:特性与实例
- Spring-data-redis:特性与实例
- Spring-Data-Redis之实例Demo
- Spring-data-redis:特性与实例
- Shiro在springmvc中的使用(实例)
- Shiro在strust2+spring中的应用
- Shiro在Spring中的缓存配置
- spring-data-redis中的坑和误区
- spring-data-redis scan 中的cursor关闭
- redis:spring-data-redis
- spring data Redis 集成jedis操作 redis中的数据。
- redis在spring中的使用
- redis在spring中的配置
- spring boot shiro redis整合
- redis Jredis spring-data-redis
- 关于打开android studioch出现the environment virable JAVA_HOME does not point to a valid JVM installation
- 线程与进程
- LeetCode 18: 4Sum
- 坐标相关的android应用程序中获取view的位置
- 抽象类与接口
- Spring-data-redis在shiro中的实例
- IOS - 自定义cell
- 虚拟现实大会ChinaVR2015报告之-数据可视化的挑战与机遇
- ORACLE LINUX 6.3 + ORACLE 11.2.0.3 RAC + VBOX安装文档
- Python 正则表达式(分组)
- 电话号码验证
- 处理ios问题“Library not found -lPods-(someCocoapod)”
- 我的第一个JS/CSS程序
- Oracle pl/sql编程 25--调用返回结果集的存储过程