java 增删改 缓存_利用java代码操作线上redis数据(增删改查)

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更新成功");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值