1、利用spring-data-redis整合
<dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <!-- 将现有的jakarta commons logging的调用转换成lsf4j的调用。 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.6.1</version> </dependency> <!-- Hack:确保commons-logging的jar包不被引入,否则将和jcl-over-slf4j冲突 --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> <scope>provided</scope> </dependency> <!-- slf4j的实现:logback,用来取代log4j。更快、更强! --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>0.9.24</version> <scope>runtime</scope> </dependency> </dependencies>
<!-- 启用注解 --> <context:annotation-config /> <!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描 --> <context:component-scan base-package="com.jmust.weixin"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:redis.properties</value> </list> </property> </bean> <!-- redis --> <bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo" > <constructor-arg value="${redis.host}"/> <constructor-arg value="${redis.port}"/> <constructor-arg value="${redis.pass}"/> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="shardInfo" ref="jedisShardInfo"/> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean>
# Redis settings redis.host=192.168.1.41 redis.port=6379 redis.pass=123456 redis.maxIdle=300 redis.maxActive=600 redis.maxWait=1000 redis.testOnBorrow=true
@Service public class RedisServiceImpl { static final String wx_return_url = "wx_return_url"; @Resource protected RedisTemplate<Serializable, Serializable> redisTemplate; /** * * @param token * @param value */ public void set(final String token, final String value) { redisTemplate.execute(new RedisCallback<Object>() { @Override public Object doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> redisSerializer = redisTemplate.getStringSerializer(); connection.hSet(redisSerializer.serialize(wx_return_url), redisSerializer.serialize(token), redisSerializer.serialize(value)); return null; } }); } /** * * @param token * @return */ public String get(final String token) { return redisTemplate.execute(new RedisCallback<String>() { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> redisSerializer = redisTemplate.getStringSerializer(); //byte[] key = redisTemplate.getStringSerializer().serialize(token); if (connection.exists(redisSerializer.serialize(wx_return_url))) { byte[] valueByte = connection.hGet(redisSerializer.serialize(wx_return_url), redisSerializer.serialize(token)); String value = redisTemplate.getStringSerializer().deserialize(valueByte); return value; } return null; } }); } /** * * @param token * @return */ public boolean remove(final String token) { return redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> redisSerializer = redisTemplate.getStringSerializer(); if (connection.exists(redisSerializer.serialize(wx_return_url))) { return connection.hDel(redisSerializer.serialize(wx_return_url), redisSerializer.serialize(token))>0?true:false; } return false; } }); } }
@Autowired private RedisServiceImpl redisService; redisService.set(token, url);// 保存cache redisService.get(pd.getString("token")); redisService.remove(pd.getString("token"));