首页 > 编程 > Java > 正文

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

2019-11-11 06:12:42
字体:
来源:转载
供稿:网友

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="121.10.105.64" />-->        <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更新成功");	}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表