【Redis基础】SSH 中Spring-data-redis使用体验
2015-03-05 21:06
543 查看
1、项目中导入所需3个jar包
commons-pool-2.2.jar
jedis-2.3.1.jar
spring-data-redis-1.3.4.relese.jar
2、新建redis配置文件redis.properties内容为
3、新建spring配置redis的文件redis.xml,内容为
4、web配置文件中添加reids配置
5、编写接口,测试例子是网上找到的,做些点修改
7、实现类
8、测试
RedisService : com.fudabdlwj.redisclient.RedisService@2dafcbf
redis has contianed lwj
add one new key-value : lwj 1234567 expire 15000
1234567
通过redis-cli.exe查看对应数据写入成功,15000时间到达时lwj被清除。
commons-pool-2.2.jar
jedis-2.3.1.jar
spring-data-redis-1.3.4.relese.jar
2、新建redis配置文件redis.properties内容为
redis.host=127.0.0.1 #ip redis.port=6379 #端口 redis.default.db=0 redis.timeout=100000 redis.maxActive=300 redis.maxIdle=100 redis.maxWait=1000 redis.testOnBorrow=true
3、新建spring配置redis的文件redis.xml,内容为
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 加载Reids属性配置文件 --> <context:property-placeholder location="classpath:redis.properties" /> <bean id="propertyConfigurerRedis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="1" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="systemPropertiesMode" value="1" /> <property name="searchSystemEnvironment" value="true" /> <property name="locations"> <list> <value>classpath:redis.properties</value> </list> </property> </bean> <!-- jedis pool配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!-- spring data redis --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="usePool" value="true"></property> <property name="hostName" value="${redis.host}" /> <property name="port" value="${redis.port}" /> <property name="timeout" value="${redis.timeout}" /> <property name="database" value="${redis.default.db}"></property> <constructor-arg index="0" ref="jedisPoolConfig" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connectionFactory-ref="jedisConnectionFactory" p:keySerializer-ref="stringRedisSerializer"> </bean> <!--这个主要是解决用cmd查看数据时为了显示正常--> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <!--配置一个基础类(之后的业务类继承于该类)、将redisTemplate注入 --> <bean id="redisBase" abstract="true"> <property name="template" ref="redisTemplate"/> </bean> <!-- 自动扫描 : 加载构建bean,以便通过注解方式注册bean --> <context:component-scan base-package="com.fudabdlwj.redisclient" /> </beans>
4、web配置文件中添加reids配置
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml, classpath:cxfservice.xml classpath:redis.xml </param-value> </context-param>
5、编写接口,测试例子是网上找到的,做些点修改
package com.fudabdlwj.redisclient; public interface IRedisService<K, V> { public void set(K key, V value, long expiredTime); public V get(K key); public void del(K key); public Boolean check(K key,V value); }6、抽象类
package com.fudabdlwj.redisclient; import java.util.concurrent.TimeUnit; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.BoundValueOperations; import org.springframework.data.redis.core.RedisTemplate; public abstract class AbstractRedisService<K, V> implements IRedisService<K, V> { @Autowired private RedisTemplate<K, V> redisTemplate; public RedisTemplate<K, V> getRedisTemplate() { return redisTemplate; } public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) { this.redisTemplate = redisTemplate; } @Override public void set(final K key, final V value, final long expiredTime) { BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key); if (expiredTime <= 0) { valueOper.set(value); } else { valueOper.set(value, expiredTime, TimeUnit.MILLISECONDS); } } @Override public V get(final K key) { BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key); return valueOper.get(); } @Override public void del(K key) { if (redisTemplate.hasKey(key)) { redisTemplate.delete(key); } } @Override public Boolean check(K key,V value){ Boolean flag=false; if (redisTemplate.hasKey(key)) { if(value.equals(get(key))){ flag=true; } } return flag; } }
7、实现类
package com.fudabdlwj.redisclient; import org.springframework.stereotype.Service; @Service("redisService") public class RedisService extends AbstractRedisService<String, String> { }
8、测试
public static void main(String[] args) { @SuppressWarnings("resource") ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:redis.xml"); RedisService rs= (RedisService) factory.getBean("redisService"); if(rs!=null){ System.out.println("RedisService : "+rs); if(rs.check("lwj", "123456")){ //</span><span style="font-family: 微软雅黑;">lwj</span><span style="font-family: 微软雅黑;">已事先写进redis的数据</span><span style="font-family:微软雅黑;"> </span><span style="font-family:微软雅黑;"> System.out.println("redis has contianed </span><span style="font-family: 微软雅黑;">lwj 123456</span><span style="font-family: 微软雅黑;">");</span><span style="font-family:微软雅黑;"> System.out.println("add one new key-value : lwj 1234567 expire 15000"); rs.set("lwj", "123456", 15000); }else{ System.out.println("add one new key-value : lwj 123456 expire 15000"); rs.set("lwj", "1234567", 15000); } String value=rs.get("lwj"); System.out.println(value); }结果
RedisService : com.fudabdlwj.redisclient.RedisService@2dafcbf
redis has contianed lwj
add one new key-value : lwj 1234567 expire 15000
1234567
通过redis-cli.exe查看对应数据写入成功,15000时间到达时lwj被清除。
相关文章推荐
- 【Redis基础】SSH 中Spring-data-redis使用体验
- SSH 中Spring-data-redis使用(使用baseService)
- 在spring data jpa中使用redis的通用list及entity存储方法
- 使用Spring-data-redis操作Redis的Sentinel
- 使用spring-data-redis开发redis应用
- 在Spring-data-Redis中如何使用特定的库?
- 使用Spring Data +Redis实现缓存
- spring-data-redis 使用
- jedis,spring-redis-data 整合使用,版本问题异常以及解决。
- Spring Data Redis 使用
- Spring+redis,spring-data-redis使用
- 使用Spring-Data-Redis存储对象(redisTemplate)
- spring data redis使用示例
- 使用spring-data-redis操作redis
- 使用spring-data-redis操作redis
- spring-data-redis 使用过程中需要注意的一点
- 6、redis之使用spring-data-redis的Template
- 使用spring-data-redis操作Redis的Sentinel
- spring-data-redis的使用优化
- Spring-data-redis配置及使用示例