Redis(RedisTemplate)运算、算法(incr、decr、increment)
生活随笔
收集整理的這篇文章主要介紹了
Redis(RedisTemplate)运算、算法(incr、decr、increment)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
RedisTemplate配置:https://www.cnblogs.com/weibanggang/p/10188682.html
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);
//設(shè)值
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"));
}
}
總結(jié)
以上是生活随笔為你收集整理的Redis(RedisTemplate)运算、算法(incr、decr、increment)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux crypto相关知识的汇总
- 下一篇: STL源码剖析 数值算法 accumul