pom.xml (redis 整合spring 相关依赖)
org.springframework.data
spring-data-redis
1.6.2.RELEASE
redis.clients
jedis
2.7.2
spring-redis.xml 配置文件
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
删除,修改测试:
package com.redis.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.util.concurrent.TimeUnit;
/**
* 修改线上redis缓存数据
* Created by qixuan.chen on 2017/1/17.
*/
public class TestRedis {
public static ApplicationContext applicationContext = null;
/**
* 主程序
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TestRedis t = new TestRedis();
//根据key修改线上redis数据 ----- 小心
//onlineData();
//本地测试数据
testData();//
}
private static void testData() {
ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext("classpath:spring-redis.xml");
final RedisTemplate redisTemplate = appCtx.getBean("redisTemplate",RedisTemplate.class);
//添加一个 key
// ValueOperations value = redisTemplate.opsForValue();
int tempTime = 604800000;
System.out.println("==3=="+redisTemplate.opsForValue().get("APPUSER_VIEW_COUNT_e548fdc05709831501570a78f6150a86"));
redisTemplate.opsForValue().set("APPUSER_VIEW_COUNT_e548fdc05709831501570a78f6150a86",342);
//设置key的生命周期
//redisTemplate.expire("APPUSER_VIEW_COUNT_e548fdc05709831501570a78f6150a86",tempTime, TimeUnit.SECONDS);
System.out.println("===="+redisTemplate.opsForValue().get("ADVERTISE_VIEW_COUNT_e548fdc05621ae83015621b3bdf20000"));
System.out.println("==2=="+redisTemplate.opsForValue().get("ADVERTISE_CLICK_COUNT_e548fdc058dcffea015919b27821260e"));
System.out.println("==3=="+redisTemplate.opsForValue().get("APPUSER_VIEW_COUNT_e548fdc05709831501570a78f6150a86"));
}
private static void onlineData() {
String viewCountKey ="ADVERTISE_VIEW_COUNT_e548fdc05990e85f0159921ad206685f";
int viewCount = 110256;
String clickCountKey ="ADVERTISE_CLICK_COUNT_e548fdc05990e85f0159921ad206685f";
int clickCount = 10146;
ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext("spring-redis.xml");
final RedisTemplate redisTemplate = appCtx.getBean("redisTemplate",RedisTemplate.class);
//添加一个 key
//ValueOperations value = redisTemplate.opsForValue();
redisTemplate.opsForValue().set(viewCountKey, viewCount);
System.out.println("1更新成功");
redisTemplate.opsForValue().set(clickCountKey, clickCount);
System.out.println("2更新成功");
}
}