import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@RestController
@Api(tags = "TestController")
@RequestMapping("/test")
@Slf4j
public class TestController {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@ApiOperation("test")
@RequestMapping(value = "/test", method = RequestMethod.GET)
public void test(String key, String hashkey, String value) {
log.info("key:{},hashkey:{},value:{}", key, hashkey, value);
{
stringRedisTemplate.opsForValue().set(key, value);
stringRedisTemplate.opsForValue().setIfAbsent(key, value);
stringRedisTemplate.opsForValue().increment(key, 2);
String value_out = stringRedisTemplate.opsForValue().get(key);
log.info("value_out:{}", value_out);
Boolean hasKey = stringRedisTemplate.hasKey(key);//判断是否有key所对应的值,有则返回true,没有则返回false
Boolean expire = stringRedisTemplate.expire(key, 60, TimeUnit.SECONDS);
// Boolean expireAt = stringRedisTemplate.expireAt(key, new Date());
byte[] dump = stringRedisTemplate.dump(key);
Boolean delete = stringRedisTemplate.delete(key);
}
{//集合
Long add = stringRedisTemplate.opsForSet().add(key, value, value + "1", value + "2");
Long remove = stringRedisTemplate.opsForSet().remove(key, value);
Long size = stringRedisTemplate.opsForSet().size(key);
Boolean isMember = stringRedisTemplate.opsForSet().isMember(key, value);
Set<String> members = stringRedisTemplate.opsForSet().members(key);
stringRedisTemplate.delete(key);
}
{//有序集合
stringRedisTemplate.opsForZSet().add(key, value, 0);
stringRedisTemplate.opsForZSet().add(key, value + 1, 1);
stringRedisTemplate.opsForZSet().add(key, value + 2, 2);
Double score = stringRedisTemplate.opsForZSet().score(key, value);
Set<String> rangeByScore = stringRedisTemplate.opsForZSet().rangeByScore(key, 0, 2);
log.info("rangeByScore:{}", rangeByScore);
stringRedisTemplate.delete(key);
}
{//哈希
stringRedisTemplate.opsForHash().put(key, hashkey, value);
stringRedisTemplate.opsForHash().putIfAbsent(key, hashkey, value);
Map<String, String> stringStringHashMap = new HashMap<>();
stringStringHashMap.put("1", "2");
stringStringHashMap.put("3", "4");
stringRedisTemplate.opsForHash().putAll(key, stringStringHashMap);
stringRedisTemplate.opsForHash().increment(key, hashkey, 2);
stringRedisTemplate.opsForHash().hasKey(key, hashkey);
Object o = stringRedisTemplate.opsForHash().get(key, hashkey);
log.info("o:{}", o);
Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(key);
log.info("entries:{}", entries);
Set<Object> keys = stringRedisTemplate.opsForHash().keys(key);
log.info("keys:{}", keys);
List<Object> values = stringRedisTemplate.opsForHash().values(key);
log.info("values:{}", values);
stringRedisTemplate.delete(key);
}
{//列表
stringRedisTemplate.opsForList().leftPush(key, value);
stringRedisTemplate.opsForList().leftPushIfPresent(key, value);
stringRedisTemplate.opsForList().leftPush(key, value, value);//如果pivot处值存在则在pivot前面添加
stringRedisTemplate.opsForList().leftPushAll(key, value, value + 1, value + 2);
stringRedisTemplate.opsForList().rightPush(key, value);
stringRedisTemplate.opsForList().set(key, 0, value);//设置指定索引处元素的值
String index = stringRedisTemplate.opsForList().index(key, 0);
log.info("index:{}", index);
List<String> range = stringRedisTemplate.opsForList().range(key, 0, 2);
log.info("range:{}", range);
}
}
}