Redis+Springmvc搭建(附windows下安装)

作者注:本文主要用于个人学习.同时欢迎交流讨论

1.添加maven依赖:

<dependency>
          <groupId>org.springframework.data</groupId>
          <artifactId>spring-data-redis</artifactId>
          <version>1.*.*.RELEASE</version>
</dependency>

2.配置redis.properties

redis.host=127.0.0.1
redis.port=6379
redis.pass=***
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true

maxIdle,最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。
MaxActive,连接池的最大数据库连接数。设为0表示无限制。
maxWait ,最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。

*maxActive:最大连接数据库连接数,设 0 为没有限制

*maxIdle:最大等待连接中的数量,设 0 为没有限制

*maxWait:最大等待毫秒数, 单位为 ms, 超过时间会出错误信息

testOnBorrow此处还不理解什么意思

3.spring配置文件引入

<context:property-placeholder location="/**/redis.properties" />
<bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        <property name="maxIdle" value="${redis.maxIdle}" />  
        <property name="maxTotal" value="${redis.maxActive}" />  
        <property name="maxWaitMillis" value="${redis.maxWait}" />  
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
</bean> 
 <bean id="stringRedisSerializer"  
        class="org.springframework.data.redis.serializer.StringRedisSerializer">  
    </bean>
    <bean id="jdkSerializationRedisSerializer"   class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
     
    <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
        p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"  p:pool-config-ref="redisPoolConfig"/>  
   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
        p:connection-factory-ref="redisConnectionFactory" p:keySerializer-ref="stringRedisSerializer" p:valueSerializer-ref="stringRedisSerializer" />
4.代码操作
Controller:@Resource RedisTemplate<String, String> redisTemplate;
redisTemplate.opsForList().leftPush(key, value);//从左向右存压栈
redisTemplate.opsForList().leftPop(key);//从左出栈
redisTemplate.opsForList().size(key);//队/栈长
redisTemplate.opsForList().range(key, start, end);//范围检索,返回List
redisTemplate.opsForList().remove(key, i, value);//移除key中值为value的i个,返回删除的个数;如果没有这个元素则返回0
redisTemplate.opsForList().index(key, index);//检索
redisTemplate.opsForList().set(key, index, value);//赋值
redisTemplate.opsForList().trim(key, start, end);//裁剪,void,删除除了[start,end]以外的所有元素 
redisTemplate.opsForList().rightPopAndLeftPush(String sourceKey, String destinationKey);//将源key的队列的右边的一个值删除,然后塞入目标key的队列的左边,返回这个值
注意:要缓存的对象必须实现Serializable接口,因为 Spring 会将对象先序列化再存入 Redis,否则报异常nested exception is java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable……

redis简介:

redis将数据缓存在内存中,周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。磁盘上保存的位置:打开redis.windows.conf文件,查找dbfilename,作者是dump.rdb文件

redis安装:

作者是用windows测试的

根据参考文章7提供的地址,作者没有下载下来,原因不详.以前在其他地方有下载存在了百度云盘里,就直接下载用了.版本是Redis-x64-3.0.504.有需要的读者请与本文作者联系

下载好后进入Redis-x64-3.0.504目录,执行redis-server.exe(或cmd下执行redis-server),出现如下界面,表示成功

 

执行redis-cli命令,可打开自带的客户端进行操作,出现127.0.0.1:6379>表示成功.具体操作命令网上一大堆!

参考文章:
1.http://blog.csdn.net/it_zhaonan/article/details/50648266
2.http://blog.csdn.net/it_man/article/details/9730667
3.http://blog.csdn.net/defonds/article/details/48716161/
4.http://www.cnblogs.com/dennisit/p/3614521.html
5.http://blog.csdn.net/junlong750/article/details/52775822
6.http://baike.baidu.com/link?url=e-SpGF0pqCA8MGI-e4Wyww32JQz35DWkwEom8VjNqh6-2TMdjBnPv7hWaM_UeEtg768OtjMoMoQIRl1aDryLI_
7.http://blog.csdn.net/renfufei/article/details/38474435/
8.http://blog.163.com/asd_wll/blog/static/21031040201332443820565/
原文地址:https://www.cnblogs.com/yanan7890/p/6492252.html