redis的propertis配置文件spring.propeties:
redis.url=127.0.0.1
redis.port=6379
applicationContext.xml中的配置:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:spring.properties</value>
</property>
<property name="systemPropertiesMode">
<value>1</value>
</property>
<property name="searchSystemEnvironment">
<value>true</value>
</property>
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
</bean>
<!-- Jedis ConnectionFactory -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.url}" p:port="${redis.port}" >
</bean>
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer">
</bean>
<!-- redis template definition -->
<bean id="redisTemplate" name="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory" p:keySerializer-ref="stringRedisSerializer"
p:hashKeySerializer-ref="stringRedisSerializer">
</bean>
代码中的应用:
1.取数据:
String verifyResult = redisTemplate.opsForValue().get(
SETTING_VERIFY + pub_openId + "_" + userident);//从redis当中获取权限校验结果
Object querySettings = redisTemplate.opsForHash().get("usersettings_",
pub_openId + userident + UserSettingType.QUERY);//从reidis当中获取用户设置的有效期
2.存数据(先删除数据、再存数据、最后设置数据保存周期):
redisTemplate.delete(redisKey);
redisTemplate.opsForValue().set(redisKey, condition);
redisTemplate.expire(redisKey, 24 * 365, TimeUnit.HOURS);