【Redis基础】SSH 中Spring-data-redis使用体验
来源:互联网 发布:电子狗数据更新 编辑:程序博客网 时间:2024/05/21 22:41
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内容为
redis.host=127.0.0.1 #ipredis.port=6379 #端口redis.default.db=0redis.timeout=100000redis.maxActive=300redis.maxIdle=100redis.maxWait=1000redis.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-placeholderlocation="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 has contianed lwj
add one new key-value : lwj 1234567 expire 15000
1234567
通过redis-cli.exe查看对应数据写入成功,15000时间到达时lwj被清除。
0 1
- 【Redis基础】SSH 中Spring-data-redis使用体验
- 【Redis基础】SSH 中Spring-data-redis使用体验
- SSH 中Spring-data-redis使用(使用baseService)
- Spring-data-redis初体验
- Spring中spring-data-redis的使用
- spring-data-redis 使用
- spring-data-redis 使用
- Spring Data Redis 使用
- spring data redis使用
- Spring+redis,spring-data-redis使用
- 使用spring-data-redis操作redis
- 使用spring-data-redis开发redis应用
- redis 与Spring Data Redis 使用
- redis:spring-data-redis
- Spring Data Redis快速使用
- Spring-Data-Redis使用文档
- spring 中 Spring-data-redis的配置与使用
- 在Spring-data-Redis中如何使用特定的库?
- BZOJ 3223 文艺平衡树 (splay)
- js执行引擎与浏览器内核分析
- ramfs--源码 inode.c
- Mysql 源码安装
- 日拱一卒(四)
- 【Redis基础】SSH 中Spring-data-redis使用体验
- 重构代码没有提高运行速度,呵呵,大家怎么样看
- Cocos2d-x3.2 双击事件
- char能表示(-128~127)
- android分享图片文字
- AndroidStudio 生成APP中文显示乱码
- SGU102 Coprimes
- 通过反编译一个简单的C程序,解释堆栈的变化
- hdu 1429 胜利大逃亡(续) bfs+状态压缩