彩虹导航个人博客
博客信息

Redis(RedisTemplate)运算、算法(incr、decr、increment)

0
发布时间:『 2020-04-22 20:18』  博客类别:redis  阅读(484) 评论(0)
package com.wbg.springRedis.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;

public class TestCal {

     static RedisTemplate redisTemplate = null;
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-redis.xml");
         redisTemplate = applicationContext.getBean(RedisTemplate.class);
        //设值
        redisTemplate.opsForValue().set("i", "10");
        print();
        //加1
        redisTemplate.getConnectionFactory().getConnection().incr(
                redisTemplate.getKeySerializer().serialize("i")
        );
        //加4
        redisTemplate.getConnectionFactory().getConnection().incrBy(
                redisTemplate.getKeySerializer().serialize("i"), 4
        );
        print();
        //
        //减1
        redisTemplate.getConnectionFactory().getConnection().decr(
                redisTemplate.getKeySerializer().serialize("i")
        );
        //减4
        redisTemplate.getConnectionFactory().getConnection().decrBy(
                redisTemplate.getKeySerializer().serialize("i"), 4
        );
        print();
        print();

        //加2.3 increment是float类型
        redisTemplate.opsForValue().increment("i",2.3);
        print();
    }
    //打印
    public static void print(){
        System.out.println(redisTemplate.opsForValue().get("i"));
    }

}
关键字:   无
评论信息
暂无评论
发表评论
验证码: 
Powered by 彩虹导航 V3.0 Copyright © 2012-2016 彩虹导航网站 版权所有