spring整合redis

redis的propertis配置文件spring.propeties:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. redis.url=127.0.0.1  
  2. redis.port=6379  

applicationContext.xml中的配置:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <bean id="propertyConfigurer"  
  2.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.         <property name="locations">  
  4.             <value>classpath:spring.properties</value>  
  5.         </property>  
  6.         <property name="systemPropertiesMode">  
  7.             <value>1</value>  
  8.         </property>  
  9.         <property name="searchSystemEnvironment">  
  10.             <value>true</value>  
  11.         </property>  
  12.         <property name="ignoreUnresolvablePlaceholders">  
  13.             <value>true</value>  
  14.         </property>  
  15.     </bean>  
  16.       
  17.     <!-- Jedis ConnectionFactory -->  
  18.     <bean id="jedisConnectionFactory"  
  19.         class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
  20.         p:host-name="${redis.url}" p:port="${redis.port}" >  
  21.     </bean>  
  22.     <bean id="stringRedisSerializer"  
  23.         class="org.springframework.data.redis.serializer.StringRedisSerializer">  
  24.     </bean>  
  25.     <!-- redis template definition -->  
  26.     <bean id="redisTemplate" name="redisTemplate"  
  27.         class="org.springframework.data.redis.core.RedisTemplate"  
  28.         p:connection-factory-ref="jedisConnectionFactory" p:keySerializer-ref="stringRedisSerializer"  
  29.         p:hashKeySerializer-ref="stringRedisSerializer">  
  30.     </bean>  

代码中的应用:

1.取数据:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. String verifyResult = redisTemplate.opsForValue().get(  
  2.                     SETTING_VERIFY + pub_openId + "_" + userident);//从redis当中获取权限校验结果  
  3.             Object querySettings = redisTemplate  
  4.                     .opsForHash()  
  5.                     .get("usersettings_",  
  6.                             pub_openId + userident + UserSettingType.QUERY);//从reidis当中获取用户设置的有效期  

2.存数据(先删除数据、再存数据、最后设置数据保存周期):

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. redisTemplate.delete(redisKey);  
  2. redisTemplate.opsForValue().set(redisKey, condition);  
  3. redisTemplate.expire(redisKey, 24 * 365, TimeUnit.HOURS);  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值