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


pom.xml (redis 整合spring 相关依赖)

		<!-- spring-redis NoSql begin -->  
		   <dependency>    
		       <groupId>org.springframework.data</groupId>    
		       <artifactId>spring-data-redis</artifactId>    
		       <version>1.6.2.RELEASE</version>    
		   </dependency>    
		   <dependency>    
		       <groupId>redis.clients</groupId>    
		       <artifactId>jedis</artifactId>    
		       <version>2.7.2</version>    
		   </dependency>    
		<!-- spring-redis NoSql end -->  

spring-redis.xml 配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--[redis-JedisPoolConfig配置](http://blog.csdn.net/liang_love_java/article/details/50510753)-->
    <!--    jedis-2.7.2.jar 依赖jar包 commons-pool2-2.3.jar
            jedis基于 commons-pool2-2.3.jar 自己实现了一个资源池。
            配置参数 详见 http://blog.csdn.net/liang_love_java/article/details/50510753
    -->
    
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="1" />
        <property name="maxTotal" value="5" />
        <property name="blockWhenExhausted" value="true" />
        <property name="maxWaitMillis" value="30000" />
        <property name="testOnBorrow" value="true" />
    </bean>

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!--<property name="hostName" value="192.168.1.200" />-->
        <property name="hostName" value="192.168.1.200" />
        <property name="port" value="6379"/>
        <!--<property name="password" value="123456"/>-->
        <property name="poolConfig" ref="jedisPoolConfig" />
        <property name="usePool" value="true"/>
        <property name="timeout" value="100000" />
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory"   ref="jedisConnectionFactory" />
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean>

</beans>

删除,修改测试:

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<String, Object> redisTemplate = appCtx.getBean("redisTemplate",RedisTemplate.class);
        //添加一个 key
//        ValueOperations<String, Object> 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<String, Object> redisTemplate = appCtx.getBean("redisTemplate",RedisTemplate.class);
        //添加一个 key
        //ValueOperations<String, Object> value = redisTemplate.opsForValue();

        redisTemplate.opsForValue().set(viewCountKey, viewCount);
        System.out.println("1更新成功");
        redisTemplate.opsForValue().set(clickCountKey, clickCount);
        System.out.println("2更新成功");
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值