spring 整合 redis
来源:互联网 发布:南方寝饰淘宝旗舰店 编辑:程序博客网 时间:2024/06/11 06:15
spring 整合 redis
- spring 整合 redis
- pomxml 配置如下
- redisconfig 配置如下
- applicationContextxml 配置如下
- redisUtiljava 代码如下
pom.xml 配置如下
<!--Redis start --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.1.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency> <!--Redis end -->
redis.config 配置如下
#redis配置redis.host=222.128.4.xxredis.port=6379redis.password=xxxredis.maxIdle=100redis.maxActive=300redis.maxWait=1000redis.testOnBorrow=trueredis.timeout=100000
applicationContext.xml 配置如下
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!-- 加载数据库配置 --> <value>classpath:datasource.properties</value> <!-- 加载redis数据库配置 --> <value>classpath:redis.properties</value> </list> </property> </bean> <!-- jedis 配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" > <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean > <!-- redis服务器中心 --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > <property name="poolConfig" ref="poolConfig" /> <property name="port" value="${redis.port}" /> <property name="hostName" value="${redis.host}" /> <property name="password" value="${redis.password}" /> <property name="timeout" value="${redis.timeout}" ></property> </bean > <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" > <property name="connectionFactory" ref="connectionFactory" /> <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 > <!-- cache配置 --> <bean id="redisUtil" class="user.cnwap.leshare.admin.util.RedisUtil" > <property name="redisTemplate" ref="redisTemplate" /> </bean >
redisUtil.java 代码如下
/** - redis cache 工具类 - */public final class RedisUtil { private Logger logger = Logger.getLogger(RedisUtil.class); private RedisTemplate<Serializable, Object> redisTemplate; /** - 批量删除对应的value - - @param keys */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } /** - 批量删除key - - @param pattern */ public void removePattern(final String pattern) { Set<Serializable> keys = redisTemplate.keys(pattern); if (keys.size() > 0) redisTemplate.delete(keys); } /** - 删除对应的value - - @param key */ public void remove(final String key) { if (exists(key)) { redisTemplate.delete(key); } } /** - 判断缓存中是否有对应的value - - @param key - @return */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } /** - 读取缓存 - - @param key - @return */ public Object get(final String key) { Object result = null; ValueOperations<Serializable, Object> operations = redisTemplate .opsForValue(); result = operations.get(key); return result; } /** - 写入缓存 - - @param key - @param value - @return */ public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate .opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** - 写入缓存 - - @param key - @param value - @return */ public boolean set(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate .opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public void setRedisTemplate( RedisTemplate<Serializable, Object> redisTemplate) { this.redisTemplate = redisTemplate; } }
- 使用redis存取java对象,对象必须实现Serializable接口。
0 0
- [Spring]Redis+Spring整合
- 深入浅出Redis-Spring整合Redis
- 深入浅出Redis-Spring整合Redis
- 深入浅出Redis-Spring整合Redis
- 深入浅出Redis-Spring整合Redis
- 深入浅出Redis-Spring整合Redis
- 深入浅出Redis-Spring整合Redis
- Redis-----5、Spring整合Redis
- spring redis整合
- Spring 整合 Redis
- spring redis整合(一)
- Spring 整合 Redis 分类:
- spring整合redis
- Spring 整合 Redis
- Spring 整合 Redis
- Redis整合Spring
- spring整合redis
- Spring 整合 Redis
- 分布式学习之一:事务
- JAVA实现SFTP上传,下载,删除等方法
- Spring 事务管理高级应用难点剖析
- 抽象类和接口的区别
- 优先队列
- spring 整合 redis
- 使用C实现模拟实现一个shell解释器(一)
- 基于matlab的车道和车道线检测样例
- webrtc的Sqrt()快速计算
- TCP的11种状态
- noi 1805 碎纸机-----搜索
- jq 获取表单未提交数据
- Java transient关键字使用小记
- pthread_cancel 与内存释放的小技巧