<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
redis:
database: 0
host: 192.168.0.198
port: 6379
password: 123456
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.StringRedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisOperator {
@Autowired
private StringRedisTemplate redisTemplate;
// Key(鍵),簡(jiǎn)單的key-value操作
/**
* 對(duì)應(yīng) -TTL key,返回給定 key的剩余生存時(shí)間(TTL, time to live)。
*/
public long ttl(String key) {
return redisTemplate.getExpire(key);
}
/**
* 對(duì)應(yīng)-expire key seconds 設(shè)置過期時(shí)間,單位秒
*/
public void expire(String key, long timeout) {
redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
}
/**
* 對(duì)應(yīng)-INCR key,增加key一次
*/
public long incr(String key, long delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 對(duì)應(yīng)-KEYS pattern,查找所有符合給定模式 pattern的 key
*/
public Set<String> keys(String pattern) {
return redisTemplate.keys(pattern);
}
/**
* 對(duì)應(yīng)-DEL key,刪除一個(gè)key
*/
public void del(String key) {
redisTemplate.delete(key);
}
// String(字符串)
/**
* 對(duì)應(yīng)-SET key value,設(shè)置一個(gè)key-value(將字符串值 value關(guān)聯(lián)到 key)
*/
public void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 對(duì)應(yīng)-SET key value EX seconds,設(shè)置key-value和超時(shí)時(shí)間(秒)
*/
public void set(String key, String value, long timeout) {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}
/**
* 對(duì)應(yīng)-GET key,返回 key所關(guān)聯(lián)的字符串值。
*/
public String get(String key) {
return (String)redisTemplate.opsForValue().get(key);
}
/**
* 批量查詢,對(duì)應(yīng)mget [key,...]
*/
public List<String> mget(List<String> keys) {
return redisTemplate.opsForValue().multiGet(keys);
}
/**
* 批量查詢,管道pipeline
*/
public List<Object> batchGet(List<String> keys) {
List<Object> result = redisTemplate.executePipelined((RedisCallback<String>) connection -> {
StringRedisConnection src = (StringRedisConnection)connection;
for (String k : keys) {
src.get(k);
}
return null;
});
return result;
}
// Hash(哈希表)
/**
* 對(duì)應(yīng)-HSET key field value,將哈希表 key中的域 field的值設(shè)為 value
*/
public void hset(String key, String field, Object value) {
redisTemplate.opsForHash().put(key, field, value);
}
/**
* 對(duì)應(yīng)-HGET key field,返回哈希表 key中給定域 field的值
*/
public String hget(String key, String field) {
return (String) redisTemplate.opsForHash().get(key, field);
}
/**
* 對(duì)應(yīng)-HDEL key field [field ...],刪除哈希表 key 中的一個(gè)或多個(gè)指定域,不存在的域?qū)⒈缓雎浴? */
public void hdel(String key, Object... fields) {
redisTemplate.opsForHash().delete(key, fields);
}
/**
* 對(duì)應(yīng)-HGETALL key,返回哈希表 key中,所有的域和值。
*/
public Map<Object, Object> hgetall(String key) {
return redisTemplate.opsForHash().entries(key);
}
// List(列表)
/**
* 對(duì)應(yīng)-LPUSH key value,將一個(gè)值 value插入到列表 key的表頭
*/
public long lpush(String key, String value) {
return redisTemplate.opsForList().leftPush(key, value);
}
/**
* 對(duì)應(yīng)-LPOP key,移除并返回列表 key的頭元素。
*/
public String lpop(String key) {
return redisTemplate.opsForList().leftPop(key);
}
/**
* 對(duì)應(yīng)-RPUSH key value,將一個(gè)值 value插入到列表 key的表尾(最右邊)。
*/
public long rpush(String key, String value) {
return redisTemplate.opsForList().rightPush(key, value);
}
}
@Api(tags = "Redis測(cè)驗(yàn)")
@RestController
@RequestMapping("redis")
public class RedisController {
@Autowired
private RedisOperator redisOperator;
@GetMapping("set")
public Result set(String key,String value){
redisOperator.set(key,value);
return Result.success();
}
@GetMapping("get")
public Result get(String key){
return Result.success(redisOperator.get(key));
}
@GetMapping("del")
public Result del(String key){
redisOperator.del(key);
return Result.success();
}
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。