Jedis操作單節點redis,叢集及redisTemplate操作redis叢集(一)
1,該文章主要介紹如何使用Jedis操作單節點redis資料,後續兩篇文章將介紹Jedis操作redis叢集及redisTemplate操作redis叢集;碼雲上面地址:https://gitee.com/dream21th/dream21th-redis
2,專案的pom檔案如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dream21th</groupId> <artifactId>dream21th-redis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dream21th-redis</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.41</version> </dependency> <!-- Swagger2 配置 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
3,專案的application.properties配置
#redis redis.host=192.168.1.81 redis.port=6379 redis.timeout=3 #redis.password=123456 redis.poolMaxTotal=10 redis.poolMaxIdle=10 redis.poolMaxWait=3 |
4,專案的config配置
@Service public class RedisPoolFactory extends CachingConfigurerSupport{ @Autowired RedisConfig redisConfig; /** * redis單例模式 * @return */ @Bean public JedisPool JedisPoolFactory() { JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle()); poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal()); poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000); JedisPool jp = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(), redisConfig.getTimeout()*1000, redisConfig.getPassword(), 0); return jp; } } |
5,專案主要的方法
package com.dream21th.dream21thredis.redis; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.dream21th.dream21thredis.key.KeyPrefix; import com.dream21th.dream21thredis.util.ObjectUtils; import com.dream21th.dream21thredis.util.StringUtils; import lombok.extern.slf4j.Slf4j; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.exceptions.JedisException; /** * 單機方式redis連線客戶端 * * @author dream21th * */ @Slf4j @Service("jedisClient") public class JedisClient implements IJedisClient { @Autowired private JedisPool jedisPool; /** * 獲取物件值 * */ public <T> T get(KeyPrefix prefix, String key, Class<T> clazz) { Jedis jedis = null; try { jedis = jedisPool.getResource(); //key String realKey = prefix.getPrefix() + key; String str = jedis.get(realKey); T t = ObjectUtils.stringToBean(str, clazz); return t; }finally { returnResource(jedis); } } /** * 給物件設定值 * */ public <T> boolean set(KeyPrefix prefix, String key, T value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); String str =ObjectUtils.beanToString(value); if(str == null || str.length() <= 0) { return false; } //key String realKey = prefix.getPrefix() + key; int seconds = prefix.expireSeconds(); if(seconds <= 0) { jedis.set(realKey, str); }else { jedis.setex(realKey, seconds, str); } return true; }finally { returnResource(jedis); } } @Override public <T> T getT(String key) { T value = null; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(getBytesKey(key))) { value = toT(jedis.get(getBytesKey(key))); log.debug("getObject {} = {}", key, value); } } catch (Exception e) { log.warn("getObject {} = {}", key, value, e); } finally { returnResource(jedis); } return value; } @Override public <T> String setT(String key, T value, int cacheSeconds) { String result = null; Jedis jedis = null; try { jedis = getResource(); result = jedis.set(getBytesKey(key), toBytes(value)); if (cacheSeconds != 0) { jedis.expire(key, cacheSeconds); } log.debug("setObject {} = {}", key, value); } catch (Exception e) { log.warn("setObject {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 判斷鍵值是否存在 * */ public <T> boolean exists(KeyPrefix prefix, String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); //key String realKey = prefix.getPrefix() + key; return jedis.exists(realKey); }finally { returnResource(jedis); } } /** * 增加一個值 * */ public <T> Long incr(KeyPrefix prefix, String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); //key String realKey = prefix.getPrefix() + key; return jedis.incr(realKey); }finally { returnResource(jedis); } } /** * 減少一個值 * */ public <T> Long decr(KeyPrefix prefix, String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); //key String realKey = prefix.getPrefix() + key; return jedis.decr(realKey); }finally { returnResource(jedis); } } /** * 獲取快取 * * @param key * 鍵 * @return 值 */ public String get(String key) { String value = null; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(key)) { value = jedis.get(key); value = StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null; log.debug("get {} = {}", key, value); } } catch (Exception e) { log.warn("get {} = {}", key, value, e); } finally { returnResource(jedis); } return value; } /** * 獲取快取 * * @param key * 鍵 * @return 值 */ public Object getObject(String key) { Object value = null; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(getBytesKey(key))) { value = toObject(jedis.get(getBytesKey(key))); log.debug("getObject {} = {}", key, value); } } catch (Exception e) { log.warn("getObject {} = {}", key, value, e); } finally { returnResource(jedis); } return value; } /** * 設定快取 * * @param key * 鍵 * @param value * 值 * @param cacheSeconds * 超時時間,0為不超時 * @return */ public String set(String key, String value, int cacheSeconds) { String result = null; Jedis jedis = null; try { jedis = getResource(); result = jedis.set(key, value); if (cacheSeconds != 0) { jedis.expire(key, cacheSeconds); } log.debug("set {} = {}", key, value); } catch (Exception e) { log.warn("set {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 設定快取 * * @param key * 鍵 * @param value * 值 * @param cacheSeconds * 超時時間,0為不超時 * @return */ public String setObject(String key, Object value, int cacheSeconds) { String result = null; Jedis jedis = null; try { jedis = getResource(); result = jedis.set(getBytesKey(key), toBytes(value)); if (cacheSeconds != 0) { jedis.expire(key, cacheSeconds); } log.debug("setObject {} = {}", key, value); } catch (Exception e) { log.warn("setObject {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 獲取List快取 * * @param key * 鍵 * @return 值 */ public List<String> getList(String key) { List<String> value = null; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(key)) { value = jedis.lrange(key, 0, -1); log.debug("getList {} = {}", key, value); } } catch (Exception e) { log.warn("getList {} = {}", key, value, e); } finally { returnResource(jedis); } return value; } /** * 獲取List快取 * * @param key * 鍵 * @return 值 */ public List<Object> getObjectList(String key) { List<Object> value = null; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(getBytesKey(key))) { List<byte[]> list = jedis.lrange(getBytesKey(key), 0, -1); value = new ArrayList<Object>(); for (byte[] bs : list) { value.add(toObject(bs)); } log.debug("getObjectList {} = {}", key, value); } } catch (Exception e) { log.warn("getObjectList {} = {}", key, value, e); } finally { returnResource(jedis); } return value; } /** * 設定List快取 * * @param key * 鍵 * @param value * 值 * @param cacheSeconds * 超時時間,0為不超時 * @return */ public long setList(String key, List<String> value, int cacheSeconds) { long result = 0; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(key)) { jedis.del(key); } /** * 採用集合的toArray()方法直接把List集合轉換成陣列,這裡需要注意,不能這樣寫: String[] array = (String[]) * mlist.toArray(); * 這樣寫的話,編譯執行時會報型別無法轉換java.lang.ClassCastException的錯誤,這是為何呢,這樣寫看起來沒有問題啊 * 因為java中的強制型別轉換是針對單個物件才有效果的,而List是多物件的集合,所以將整個List強制轉換是不行的 正確的寫法應該是這樣的 */ result = jedis.rpush(key, value.toArray(new String[0])); if (cacheSeconds != 0) { jedis.expire(key, cacheSeconds); } log.debug("setList {} = {}", key, value); } catch (Exception e) { log.warn("setList {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 設定List快取 * * @param key * 鍵 * @param value * 值 * @param cacheSeconds * 超時時間,0為不超時 * @return */ public long setObjectList(String key, List<Object> value, int cacheSeconds) { long result = 0; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(getBytesKey(key))) { jedis.del(key); } List<byte[]> list = new ArrayList<byte[]>(); for (Object o : value) { list.add(toBytes(o)); } /*byte[][] bytes=new byte[list.size()][]; for(int i=0;i<list.size();i++) { bytes[i]=list.get(i); }*/ result = jedis.rpush(getBytesKey(key),list.toArray(new byte[0][])); if (cacheSeconds != 0) { jedis.expire(key, cacheSeconds); } log.debug("setObjectList {} = {}", key, value); } catch (Exception e) { log.warn("setObjectList {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 向List快取中新增值 * * @param key * 鍵 * @param value * 值 * @return */ public long listAdd(String key, String... value) { long result = 0; Jedis jedis = null; try { jedis = getResource(); result = jedis.rpush(key, value); log.debug("listAdd {} = {}", key, value); } catch (Exception e) { log.warn("listAdd {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 向List快取中新增值 * * @param key * 鍵 * @param value * 值 * @return */ public long listObjectAdd(String key, Object... value) { long result = 0; Jedis jedis = null; try { jedis = getResource(); List<byte[]> list = new ArrayList<byte[]>(); for (Object o : value) { list.add(toBytes(o)); } result = jedis.rpush(getBytesKey(key), list.toArray(new byte[0][])); log.debug("listObjectAdd {} = {}", key, value); } catch (Exception e) { log.warn("listObjectAdd {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 獲取快取 * * @param key * 鍵 * @return 值 */ public Set<String> getSet(String key) { Set<String> value = null; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(key)) { value = jedis.smembers(key); log.debug("getSet {} = {}", key, value); } } catch (Exception e) { log.warn("getSet {} = {}", key, value, e); } finally { returnResource(jedis); } return value; } /** * 獲取快取 * * @param key * 鍵 * @return 值 */ public Set<Object> getObjectSet(String key) { Set<Object> value = null; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(getBytesKey(key))) { value = new HashSet<Object>(); Set<byte[]> set = jedis.smembers(getBytesKey(key)); for (byte[] bs : set) { value.add(toObject(bs)); } log.debug("getObjectSet {} = {}", key, value); } } catch (Exception e) { log.warn("getObjectSet {} = {}", key, value, e); } finally { returnResource(jedis); } return value; } /** * 設定Set快取 * * @param key * 鍵 * @param value * 值 * @param cacheSeconds * 超時時間,0為不超時 * @return */ public long setSet(String key, Set<String> value, int cacheSeconds) { long result = 0; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(key)) { jedis.del(key); } result = jedis.sadd(key, (String[]) value.toArray()); if (cacheSeconds != 0) { jedis.expire(key, cacheSeconds); } log.debug("setSet {} = {}", key, value); } catch (Exception e) { log.warn("setSet {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 設定Set快取 * * @param key * 鍵 * @param value * 值 * @param cacheSeconds * 超時時間,0為不超時 * @return */ public long setObjectSet(String key, Set<Object> value, int cacheSeconds) { long result = 0; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(getBytesKey(key))) { jedis.del(key); } Set<byte[]> set = new HashSet<byte[]>(); for (Object o : value) { set.add(toBytes(o)); } result = jedis.sadd(getBytesKey(key), (byte[][]) set.toArray()); if (cacheSeconds != 0) { jedis.expire(key, cacheSeconds); } log.debug("setObjectSet {} = {}", key, value); } catch (Exception e) { log.warn("setObjectSet {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 向Set快取中新增值 * * @param key * 鍵 * @param value * 值 * @return */ public long setSetAdd(String key, String... value) { long result = 0; Jedis jedis = null; try { jedis = getResource(); result = jedis.sadd(key, value); log.debug("setSetAdd {} = {}", key, value); } catch (Exception e) { log.warn("setSetAdd {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 向Set快取中新增值 * * @param key * 鍵 * @param value * 值 * @return */ public long setSetObjectAdd(String key, Object... value) { long result = 0; Jedis jedis = null; try { jedis = getResource(); Set<byte[]> set = new HashSet<byte[]>(); for (Object o : value) { set.add(toBytes(o)); } result = jedis.rpush(getBytesKey(key), set.toArray(new byte[0][])); log.debug("setSetObjectAdd {} = {}", key, value); } catch (Exception e) { log.warn("setSetObjectAdd {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 獲取Map快取 * * @param key * 鍵 * @return 值 */ public Map<String, String> getMap(String key) { Map<String, String> value = null; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(key)) { value = jedis.hgetAll(key); log.debug("getMap {} = {}", key, value); } } catch (Exception e) { log.warn("getMap {} = {}", key, value, e); } finally { returnResource(jedis); } return value; } /** * 獲取Map快取 * * @param key * 鍵 * @return 值 */ public Map<String, Object> getObjectMap(String key) { Map<String, Object> value = null; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(getBytesKey(key))) { value = new HashMap<String, Object>(); Map<byte[], byte[]> map = jedis.hgetAll(getBytesKey(key)); for (Map.Entry<byte[], byte[]> e : map.entrySet()) { value.put(StringUtils.toString(e.getKey()), toObject(e.getValue())); } log.debug("getObjectMap {} = {}", key, value); } } catch (Exception e) { log.warn("getObjectMap {} = {}", key, value, e); } finally { returnResource(jedis); } return value; } /** * 設定Map快取 * * @param key * 鍵 * @param value * 值 * @param cacheSeconds * 超時時間,0為不超時 * @return */ public String setMap(String key, Map<String, String> value, int cacheSeconds) { String result = null; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(key)) { jedis.del(key); } result = jedis.hmset(key, value); if (cacheSeconds != 0) { jedis.expire(key, cacheSeconds); } log.debug("setMap {} = {}", key, value); } catch (Exception e) { log.warn("setMap {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 設定Map快取 * * @param key * 鍵 * @param value * 值 * @param cacheSeconds * 超時時間,0為不超時 * @return */ public String setObjectMap(String key, Map<String, Object> value, int cacheSeconds) { String result = null; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(getBytesKey(key))) { jedis.del(key); } Map<byte[], byte[]> map = new HashMap<byte[], byte[]>(); for (Map.Entry<String, Object> e : value.entrySet()) { map.put(getBytesKey(e.getKey()), toBytes(e.getValue())); } result = jedis.hmset(getBytesKey(key), (Map<byte[], byte[]>) map); if (cacheSeconds != 0) { jedis.expire(key, cacheSeconds); } log.debug("setObjectMap {} = {}", key, value); } catch (Exception e) { log.warn("setObjectMap {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 向Map快取中新增值 * * @param key * 鍵 * @param value * 值 * @return */ public String mapPut(String key, Map<String, String> value) { String result = null; Jedis jedis = null; try { jedis = getResource(); result = jedis.hmset(key, value); log.debug("mapPut {} = {}", key, value); } catch (Exception e) { log.warn("mapPut {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 向Map快取中新增值 * * @param key * 鍵 * @param value * 值 * @return */ public String mapObjectPut(String key, Map<String, Object> value) { String result = null; Jedis jedis = null; try { jedis = getResource(); Map<byte[], byte[]> map = new HashMap<byte[], byte[]>(); for (Map.Entry<String, Object> e : value.entrySet()) { map.put(getBytesKey(e.getKey()), toBytes(e.getValue())); } result = jedis.hmset(getBytesKey(key), (Map<byte[], byte[]>) map); log.debug("mapObjectPut {} = {}", key, value); } catch (Exception e) { log.warn("mapObjectPut {} = {}", key, value, e); } finally { returnResource(jedis); } return result; } /** * 移除Map快取中的值 * * @param key * 鍵 * @param value * 值 * @return */ public long mapRemove(String key, String mapKey) { long result = 0; Jedis jedis = null; try { jedis = getResource(); result = jedis.hdel(key, mapKey); log.debug("mapRemove {} {}", key, mapKey); } catch (Exception e) { log.warn("mapRemove {} {}", key, mapKey, e); } finally { returnResource(jedis); } return result; } /** * 移除Map快取中的值 * * @param key * 鍵 * @param value * 值 * @return */ public long mapObjectRemove(String key, String mapKey) { long result = 0; Jedis jedis = null; try { jedis = getResource(); result = jedis.hdel(getBytesKey(key), getBytesKey(mapKey)); log.debug("mapObjectRemove {} {}", key, mapKey); } catch (Exception e) { log.warn("mapObjectRemove {} {}", key, mapKey, e); } finally { returnResource(jedis); } return result; } /** * 判斷Map快取中的Key是否存在 * * @param key * 鍵 * @param value * 值 * @return */ public boolean mapExists(String key, String mapKey) { boolean result = false; Jedis jedis = null; try { jedis = getResource(); result = jedis.hexists(key, mapKey); log.debug("mapExists {} {}", key, mapKey); } catch (Exception e) { log.warn("mapExists {} {}", key, mapKey, e); } finally { returnResource(jedis); } return result; } /** * 判斷Map快取中的Key是否存在 * * @param key * 鍵 * @param value * 值 * @return */ public boolean mapObjectExists(String key, String mapKey) { boolean result = false; Jedis jedis = null; try { jedis = getResource(); result = jedis.hexists(getBytesKey(key), getBytesKey(mapKey)); log.debug("mapObjectExists {} {}", key, mapKey); } catch (Exception e) { log.warn("mapObjectExists {} {}", key, mapKey, e); } finally { returnResource(jedis); } return result; } /** * 刪除快取 * * @param key * 鍵 * @return */ public long del(String key) { long result = 0; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(key)) { result = jedis.del(key); log.debug("del {}", key); } else { log.debug("del {} not exists", key); } } catch (Exception e) { log.warn("del {}", key, e); } finally { returnResource(jedis); } return result; } /** * 刪除快取 * * @param key * 鍵 * @return */ public long delObject(String key) { long result = 0; Jedis jedis = null; try { jedis = getResource(); if (jedis.exists(getBytesKey(key))) { result = jedis.del(getBytesKey(key)); log.debug("delObject {}", key); } else { log.debug("delObject {} not exists", key); } } catch (Exception e) { log.warn("delObject {}", key, e); } finally { returnResource(jedis); } return result; } /** * 快取是否存在 * * @param key * 鍵 * @return */ public boolean exists(String key) { boolean result = false; Jedis jedis = null; try { jedis = getResource(); result = jedis.exists(key); log.debug("exists {}", key); } catch (Exception e) { log.warn("exists {}", key, e); } finally { returnResource(jedis); } return result; } /** * 快取是否存在 * * @param key * 鍵 * @return */ public boolean existsObject(String key) { boolean result = false; Jedis jedis = null; try { jedis = getResource(); result = jedis.exists(getBytesKey(key)); log.debug("existsObject {}", key); } catch (Exception e) { log.warn("existsObject {}", key, e); } finally { returnResource(jedis); } return result; } /** * 獲取資源 * * @return * @throws JedisException */ public Jedis getResource() throws JedisException { Jedis jedis = null; try { jedis = jedisPool.getResource(); // log.debug("getResource.", jedis); } catch (JedisException e) { log.warn("getResource.", e); returnBrokenResource(jedis); throw e; } return jedis; } /** * 歸還資源 * * @param jedis * @param isBroken */ public void returnBrokenResource(Jedis jedis) { if (jedis != null) { // jedisPool.returnBrokenResource(jedis); jedis.close(); } } /** * 釋放資源 * * @param jedis * @param isBroken */ public void returnResource(Jedis jedis) { if (jedis != null) { // jedisPool.returnResource(jedis); jedis.close(); } } /** * 獲取byte[]型別Key * * @param key * @return */ public static byte[] getBytesKey(Object object) { if (object instanceof String) { return StringUtils.getBytes((String) object); } else { return ObjectUtils.serialize(object); } } /** * Object轉換byte[]型別 * * @param key * @return */ public static byte[] toBytes(Object object) { return ObjectUtils.serialize(object); } /** * byte[]型轉換Object * * @param key * @return */ public static Object toObject(byte[] bytes) { return ObjectUtils.unserialize(bytes); } /** * byte[]型轉換T * * @param key * @return */ public static <T> T toT(byte[] bytes) { return ObjectUtils.unserializeT(bytes); } } |
6,controller測試
package com.dream21th.dream21thredis.controller; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.dream21th.dream21thredis.dto.Person; import com.dream21th.dream21thredis.dto.Student; import com.dream21th.dream21thredis.key.UserKey; import com.dream21th.dream21thredis.redis.IJedisClient; import com.dream21th.dream21thredis.result.Result; import com.dream21th.dream21thredis.service.RedisService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; @Slf4j @RestController @RequestMapping("/singleNode") @Api(tags="單節點redis操作") public class RedisSingleNodeController { @Autowired private IJedisClient jedisClient; @Autowired private RedisService redisService; @PostMapping("/mapTest/{key}") @ApiOperation(value = "map測試", notes = "map測試") public Result<Map<String,String>> mapTest(@PathVariable("key") String key,@RequestBody Map<String,String> map){ jedisClient.setMap(key, map,0); return Result.success(jedisClient.getMap(key)); } @PostMapping("/listTest/{key}") public Result<List<String>> ListTest(@PathVariable("key") String key,@RequestBody List<String> list){ jedisClient.setList(key, list, 0); return Result.success(jedisClient.getList(key)); } @PostMapping("/listTestObj/{key}") public Result<List<Object>> listTestObj(@PathVariable("key") String key,@RequestBody List<Object> list){ jedisClient.setObjectList(key, list, 0); return Result.success(jedisClient.getObjectList(key)); } @PostMapping("/listTestT") public Result<Student> listTestT(@RequestBody Student t){ jedisClient.set(UserKey.getById, "name",t); log.info("鍵值是否存在:{}",jedisClient.exists(UserKey.getById, "name")); redisService.getResult(); return Result.success(jedisClient.get(UserKey.getById, "name",Student.class)); } @PostMapping("/decIcrTest") public Result<Long> decIcrTest(long num){ jedisClient.set(UserKey.getById, "count",num); log.info("鍵值是否存在:{}",jedisClient.exists(UserKey.getById, "count")); log.info("加1後值:{}",jedisClient.incr(UserKey.getById, "count")); log.info("減1後值:{}",jedisClient.decr(UserKey.getById, "count")); return Result.success(jedisClient.get(UserKey.getById, "count",Long.class)); } @PostMapping("/testObj") public Result<Student> testObj(@RequestBody Student t){ jedisClient.setObject("student", t, 0); return Result.success((Student)jedisClient.getObject("student")); } @PostMapping("/testT") public Result<Student> testT(@RequestBody Student t){ jedisClient.setObject("student", t, 0); return Result.success(jedisClient.getT("student")); } @PostMapping("/testT1") public Result<Person> testT1(@RequestBody Person t){ jedisClient.setT("person", t, 0); return Result.success(jedisClient.getT("person")); } @PostMapping("/testList") public Result<List<Student>> testList(@RequestBody Student t){ return Result.success(redisService.getList()); } @PostMapping("/mapTestT") @ApiOperation(value = "傳入物件,以map獲取值", notes = "傳入物件,以map獲取值") public Result<Map> mapTestT(@RequestBody Student t){ jedisClient.set(UserKey.getById, "map",t); log.info("鍵值是否存在:{}",jedisClient.exists(UserKey.getById, "map")); redisService.getResult(); return Result.success(jedisClient.get(UserKey.getById, "map",Map.class)); } } |
相關文章
- Jedis使用連線池操作redis叢集Redis
- 【Redis】用python操作redis叢集RedisPython
- java操作Redis3.0.6叢集JavaRedisS3
- Redis操作及叢集搭建以及高可用配置Redis
- java操作redis叢集連線池JavaRedis
- Redis系列:搭建Redis叢集(叢集模式)Redis模式
- redis cluster 叢集故障恢復操作思路Redis
- redis與叢集實用操作筆記Redis筆記
- Redis服務之叢集節點管理Redis
- Redis Manager 叢集管理與節點管理Redis
- redis叢集Redis
- redis 叢集Redis
- redis叢集原理Redis
- redis叢集搭建Redis
- 搭建 Redis 叢集Redis
- Redis cluster 叢集Redis
- redis系列:叢集Redis
- Redis Cluster(叢集)Redis
- redis偽叢集配置Cluster叢集模式Redis模式
- jedis操作 redisRedis
- 認識Redis叢集——Redis ClusterRedis
- redis原理及叢集主從配置Redis
- Docker 容器搭建及 Redis 叢集原理DockerRedis
- 檢視Redis叢集所有節點記憶體工具Redis記憶體
- Redis叢集搭建與簡單使用Redis
- Redis(單機&叢集)Pipeline工具類Redis
- 搭建redis cluster叢集Redis
- redis 叢集構建Redis
- redis叢集報錯Redis
- redis 4.0.11 叢集搭建Redis
- Redis--叢集搭建Redis
- docker-redis叢集DockerRedis
- redis叢集的搭建Redis
- Docker搭建Redis叢集DockerRedis
- Redis(5.0) 叢集搭建Redis
- 搭建Redis原生叢集Redis
- redis叢集指導Redis
- DKHhadoop叢集新增節點管理功能的操作步驟Hadoop