springboot整合redis(stringRedisTemplate)
非关系型数据库: redis:资料 阿靖哦
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
operation
HashOperations<String, String, String> ops1 = stringRedisTemplate.opsForHash();
ListOperations<String, String> ops2 = stringRedisTemplate.opsForList();
SetOperations<String, String> ops3 = stringRedisTemplate.opsForSet();
ZSetOperations<String, String> ops4 = stringRedisTemplate.opsForZSet();
1.操作字符串
stringRedisTemplate.opsForValue().set("test","100",60*10,TimeUnit.SECOND;
stringRedisTemplate.opsForValue().get("test")
2.操作hash
stringRedisTemplate.opsForHash().put(key, hashKey, value);
stringRedisTemplate.opsForHash().entries(key)
3.操作list
stringRedisTemplate.opsForList().rightPush(key, value)
end = stringRedisTemplate.opsForList().size(key)
stringRedisTemplate.opsForList().range(key, start, end)
4.操作set
stringRedisTemplate.opsForSet().add("red_123", "1","2","3");
stringRedisTemplate.opsForSet().isMember("red_123", "1")
stringRedisTemplate.opsForSet().members("red_123")
stringRedisTemplate.opsForSet().remove(key, values)
5.操作有序set
stringRedisTemplate.opsForZSet().add(key, tuples)
stringRedisTemplate.opsForZSet().rangeByScore(key, min, max)
stringRedisTemplate
//val做-1操作int类型
stringRedisTemplate.boundValueOps("test").increment(-1)
//val做+1操作 int类型
stringRedisTemplate.boundValueOps("test").increment(1)
//检查key是否存在,返回boolean值
stringRedisTemplate.hasKey("546545")
//根据key删除缓存
stringRedisTemplate.delete("test");
//设置过期时间
stringRedisTemplate.expire("red_123",10,TimeUnit.SECOND)
//根据key获取过期时间
stringRedisTemplate.getExpire("test")
//根据key获取过期时间并换算成指定单位
stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)
redis 客户端 (RedisClient )
package com.ssdl.sales.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* redis 客户端
*/
@Component
public class RedisClient {
@Autowired
private JedisPool jedisPool;
public Jedis getResource() {
return jedisPool.getResource();
}
@SuppressWarnings("deprecation")
public void returnResource(Jedis jedis) {
if(jedis != null){
jedisPool.returnResourceObject(jedis);
}
}
public void set(String key, String value) {
Jedis jedis=null;
try{
jedis = getResource();
jedis.set(key, value);
LOG.info("Redis set success - " + key + ", value:" + value);
} catch (Exception e) {
e.printStackTrace();
LOG.error("Redis set error: "+ e.getMessage() +" - " + key + ", value:" + value);
}finally{
returnResource(jedis);
}
}
public String get(String key) {
String result = null;
Jedis jedis=null;
try{
jedis = getResource();
result = jedis.get(key);
LOG.info("Redis get success - " + key + ", value:" + result);
} catch (Exception e) {
e.printStackTrace();
LOG.error("Redis set error: "+ e.getMessage() +" - " + key + ", value:" + result);
}finally{
returnResource(jedis);
}
return result;
}
public String expire(String key,int seconds) {
String result = null;
Jedis jedis=null;
try{
jedis = getResource();
jedis.expire(key, seconds);
LOG.info("Redis expire success - " + key + ", value:" + result);
} catch (Exception e) {
e.printStackTrace();
LOG.error("Redis seexpiret error: "+ e.getMessage() +" - " + key + ", value:" + result);
}finally{
returnResource(jedis);
}
return result;
}
}
//设置redis k v
redis.set(SALES_CHECKCODE + phone, checkCode + "");
// String seconds = PropsUtil.getV("redis.expire");
redis.expire(SALES_CHECKCODE + phone, 86400);
//获取redis k的值
String codeRedis = redis.get(SALES_CHECKCODE + phone);
//Tools.isBlank(codeRedis)判断是否有值
if (!develop && Tools.isBlank(codeRedis)) {
user.setCode(ServiceCode.CHECK_CODE_INVALID.getCode());
user.setDesc(ServiceCode.CHECK_CODE_INVALID.getName());
return user;
}