| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- package cn.com.goldenwater.dcproj.utils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.dao.DataAccessException;
- import org.springframework.data.redis.connection.RedisConnection;
- 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;
- /**
- * @author 慕课网
- * @Title: Redis 工具类
- */
- @Component
- public class RedisOperator {
- // @Autowired
- // private RedisTemplate<String, Object> redisTemplate;
- @Autowired
- private StringRedisTemplate redisTemplate;
- // Key(键),简单的key-value操作
- /**
- * 实现命令:TTL key,以秒为单位,返回给定 key的剩余生存时间(TTL, time to live)。
- *
- * @param key
- * @return
- */
- public long ttl(String key) {
- return redisTemplate.getExpire(key);
- }
- /**
- * 实现命令:expire 设置过期时间,单位秒
- *
- * @param key
- * @return
- */
- public void expire(String key, long timeout) {
- redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
- }
- /**
- * 实现命令:INCR key,增加key一次
- *
- * @param key
- * @return
- */
- public long incr(String key, long delta) {
- return redisTemplate.opsForValue().increment(key, delta);
- }
- /**
- * 实现命令:KEYS pattern,查找所有符合给定模式 pattern的 key
- */
- public Set<String> keys(String pattern) {
- return redisTemplate.keys(pattern);
- }
- /**
- * 实现命令:DEL key,删除一个key
- *
- * @param key
- */
- public void del(String key) {
- redisTemplate.delete(key);
- }
- // String(字符串)
- /**
- * 实现命令:SET key value,设置一个key-value(将字符串值 value关联到 key)
- *
- * @param key
- * @param value
- */
- public void set(String key, String value) {
- redisTemplate.opsForValue().set(key, value);
- }
- /**
- * 实现命令:SET key value EX seconds,设置key-value和超时时间(秒)
- *
- * @param key
- * @param value
- * @param timeout (以秒为单位)
- */
- public void set(String key, String value, long timeout) {
- redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
- }
- /**
- * 实现命令:GET key,返回 key所关联的字符串值。
- *
- * @param key
- * @return value
- */
- public String get(String key) {
- return (String) redisTemplate.opsForValue().get(key);
- }
- /**
- * 批量查询,对应mget
- *
- * @param keys
- * @return
- */
- public List<String> mget(List<String> keys) {
- return redisTemplate.opsForValue().multiGet(keys);
- }
- /**
- * 批量查询,管道pipeline
- *
- * @param keys
- * @return
- */
- public List<Object> batchGet(List<String> keys) {
- // nginx -> keepalive
- // redis -> pipeline
- List<Object> result = redisTemplate.executePipelined(new RedisCallback<String>() {
- @Override
- public String doInRedis(RedisConnection connection) throws DataAccessException {
- StringRedisConnection src = (StringRedisConnection) connection;
- for (String k : keys) {
- src.get(k);
- }
- return null;
- }
- });
- return result;
- }
- // Hash(哈希表)
- /**
- * 实现命令:HSET key field value,将哈希表 key中的域 field的值设为 value
- *
- * @param key
- * @param field
- * @param value
- */
- public void hset(String key, String field, Object value) {
- redisTemplate.opsForHash().put(key, field, value);
- }
- /**
- * 实现命令:HGET key field,返回哈希表 key中给定域 field的值
- *
- * @param key
- * @param field
- * @return
- */
- public String hget(String key, String field) {
- return (String) redisTemplate.opsForHash().get(key, field);
- }
- /**
- * 实现命令:HDEL key field [field ...],删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。
- *
- * @param key
- * @param fields
- */
- public void hdel(String key, Object... fields) {
- redisTemplate.opsForHash().delete(key, fields);
- }
- /**
- * 实现命令:HGETALL key,返回哈希表 key中,所有的域和值。
- *
- * @param key
- * @return
- */
- public Map<Object, Object> hgetall(String key) {
- return redisTemplate.opsForHash().entries(key);
- }
- // List(列表)
- /**
- * 实现命令:LPUSH key value,将一个值 value插入到列表 key的表头
- *
- * @param key
- * @param value
- * @return 执行 LPUSH命令后,列表的长度。
- */
- public long lpush(String key, String value) {
- return redisTemplate.opsForList().leftPush(key, value);
- }
- /**
- * 实现命令:LPOP key,移除并返回列表 key的头元素。
- *
- * @param key
- * @return 列表key的头元素。
- */
- public String lpop(String key) {
- return (String) redisTemplate.opsForList().leftPop(key);
- }
- /**
- * 实现命令:RPUSH key value,将一个值 value插入到列表 key的表尾(最右边)。
- *
- * @param key
- * @param value
- * @return 执行 LPUSH命令后,列表的长度。
- */
- public long rpush(String key, String value) {
- return redisTemplate.opsForList().rightPush(key, value);
- }
- }
|