Jedis操作单节点redis,集群及redisTemplate操作redis集群(二)
1,码云上面地址:https://gitee.com/dream21th/dream21th-redis
2,application.properties配置文件信息如下
#redisCluter redis.cluster.nodes=192.168.1.102:7000,192.168.1.102:7001,192.168.1.102:7002,192.168.1.102:7003,192.168.1.102:7004,192.168.1.102:7005 redis.cluster.testOnReturn=true redis.cluster.maxIdle=10 redis.cluster.minIdle=2 redis.cluster.maxWaitMills=3000 redis.cluster.testOnBorrow=true redis.cluster.maxTotal=4 redis.cluster.connectionTimeOut=30000 |
3,配置文件如下
/* redis集群模式 @return / @Bean public JedisCluster getJedisCluster() { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(redisConfig.getCluster().getMaxTotal()); config.setMaxIdle(redisConfig.getCluster().getMaxIdle()); config.setMaxWaitMillis(redisConfig.getCluster().getMaxWaitMills()); config.setTestOnBorrow(redisConfig.getCluster().getTestOnBorrow()); List<String> node=Arrays.asList(redisConfig.getCluster().getNodes().split(“,”)); // 集群模式 JedisPoolConfig poolConfig = new JedisPoolConfig(); Set<HostAndPort> nodes = new HashSet<HostAndPort>(); node.forEach(n->{ String[] ip=n.split(“:”); HostAndPort hostAndPort = new HostAndPort(ip[0],Integer.parseInt(ip[1])); nodes.add(hostAndPort); }); JedisCluster jedisCluster = new JedisCluster(nodes, poolConfig); return jedisCluster; } |
4,对外提供的方法
package com.dream21th.dream21thredis.redis; import java.util.List; import java.util.Map; import java.util.Set; import com.dream21th.dream21thredis.key.KeyPrefix; / @author dream21th 采用jedis操作redis数据库提供的方法 / public interface IJedisClient { / 获取缓存 @param prefix 键的前缀 @param key 键的值 @param clazz 获取对象类型 @return / public <T> T get(KeyPrefix prefix, String key, Class<T> clazz); / 设置缓存 @param prefix 键的前缀 @param key 键的值 @param value 设置的值 @return / public <T> boolean set(KeyPrefix prefix, String key, T value); / 判断缓存是否存在 @param prefix @param key @return / public <T> boolean exists(KeyPrefix prefix, String key); /** 键的值加1 @param prefix @param key @return / public <T> Long incr(KeyPrefix prefix, String key); / 键的值减1 @param prefix @param key @return */ public <T> Long decr(KeyPrefix prefix, String key); / 获取缓存 @param key 键 @return 值 / public String get(String key); / 获取缓存 @param key 键 @return 值 / public Object getObject(String key); / 获取缓存 @param key 键 @return 值 / public <T> T getT(String key); / 设置缓存 @param key 键 @param value 值 @param cacheSeconds 超时时间,0为不超时 @return / public String set(String key, String value, int cacheSeconds); / 设置缓存 @param key 键 @param value 值 @param cacheSeconds 超时时间,0为不超时 @return / public String setObject(String key, Object value, int cacheSeconds); / 设置缓存 @param key 键 @param value 值 @param cacheSeconds 超时时间,0为不超时 @return / public <T> String setT(String key, T value, int cacheSeconds); / 获取List缓存 @param key 键 @return 值 / public List<String> getList(String key); / 获取List缓存 @param key 键 @return 值 / public List<Object> getObjectList(String key); / 设置List缓存 @param key 键 @param value 值 @param cacheSeconds 超时时间,0为不超时 @return / public long setList(String key, List<String> value, int cacheSeconds); / 设置List缓存 @param key 键 @param value 值 @param cacheSeconds 超时时间,0为不超时 @return / public long setObjectList(String key, List<Object> value, int cacheSeconds); / 向List缓存中添加值 @param key 键 @param value 值 @return / public long listAdd(String key, String… value); /** 向List缓存中添加值 @param key 键 @param value 值 @return / public long listObjectAdd(String key, Object… value); / 获取缓存 @param key 键 @return 值 / public Set<String> getSet(String key); / 获取缓存 @param key 键 @return 值 / public Set<Object> getObjectSet(String key); / 设置Set缓存 @param key 键 @param value 值 @param cacheSeconds 超时时间,0为不超时 @return / public long setSet(String key, Set<String> value, int cacheSeconds); / 设置Set缓存 @param key 键 @param value 值 @param cacheSeconds 超时时间,0为不超时 @return / public long setObjectSet(String key, Set<Object> value, int cacheSeconds); / 向Set缓存中添加值 @param key 键 @param value 值 @return */ public long setSetAdd(String key, String… value); / 向Set缓存中添加值 @param key 键 @param value 值 @return / public long setSetObjectAdd(String key, Object… value); /** 获取Map缓存 @param key 键 @return 值 / public Map<String, String> getMap(String key); /** 获取Map缓存 @param key 键 @return 值 / public Map<String, Object> getObjectMap(String key); /** 设置Map缓存 @param key 键 @param value 值 @param cacheSeconds 超时时间,0为不超时 @return / public String setMap(String key, Map<String, String> value, int cacheSeconds); /** 设置Map缓存 @param key 键 @param value 值 @param cacheSeconds 超时时间,0为不超时 @return / public String setObjectMap(String key, Map<String, Object> value, int cacheSeconds); /** 向Map缓存中添加值 @param key 键 @param value 值 @return / public String mapPut(String key, Map<String, String> value); / 向Map缓存中添加值 @param key 键 @param value 值 @return */ public String mapObjectPut(String key, Map<String, Object> value); / 移除Map缓存中的值 @param key 键 @param value 值 @return / public long mapRemove(String key, String mapKey); /** 移除Map缓存中的值 @param key 键 @param value 值 @return / public long mapObjectRemove(String key, String mapKey); / 判断Map缓存中的Key是否存在 @param key 键 @param value 值 @return */ public boolean mapExists(String key, String mapKey); / 判断Map缓存中的Key是否存在 @param key 键 @param value 值 @return / public boolean mapObjectExists(String key, String mapKey); /** 删除缓存 @param key 键 @return / public long del(String key); /** 删除缓存 @param key 键 @return / public long delObject(String key); /** 缓存是否存在 @param key 键 @return / public boolean exists(String key); /** 缓存是否存在 @param key 键 @return */ public boolean existsObject(String key); } |
5,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(“/mutilNode”) @Api(tags=”多节点redis操作”) public class RedisMutilNodeController { @Autowired private IJedisClient jedisClusterClient; @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){ jedisClusterClient.setMap(key, map,0); return Result.success(jedisClusterClient.getMap(key)); } @PostMapping(“/listTest/{key}”) public Result<List<String>> ListTest(@PathVariable(“key”) String key,@RequestBody List<String> list){ jedisClusterClient.setList(key, list, 0); return Result.success(jedisClusterClient.getList(key)); } @PostMapping(“/listTestObj/{key}”) public Result<List<Object>> listTestObj(@PathVariable(“key”) String key,@RequestBody List<Object> list){ jedisClusterClient.setObjectList(key, list, 0); return Result.success(jedisClusterClient.getObjectList(key)); } @PostMapping(“/listTestT”) public Result<Student> listTestT(@RequestBody Student t){ jedisClusterClient.set(UserKey.getById, “name”,t); log.info(“键值是否存在:{}”,jedisClusterClient.exists(UserKey.getById, “name”)); redisService.getResult(); return Result.success(jedisClusterClient.get(UserKey.getById, “name”,Student.class)); } @PostMapping(“/decIcrTest”) public Result<Long> decIcrTest(long num){ jedisClusterClient.set(UserKey.getById, “count”,num); log.info(“键值是否存在:{}”,jedisClusterClient.exists(UserKey.getById, “count”)); log.info(“加1后值:{}”,jedisClusterClient.incr(UserKey.getById, “count”)); log.info(“减1后值:{}”,jedisClusterClient.decr(UserKey.getById, “count”)); return Result.success(jedisClusterClient.get(UserKey.getById, “count”,Long.class)); } @PostMapping(“/testObj”) public Result<Student> testObj(@RequestBody Student t){ jedisClusterClient.setObject(“student”, t, 0); return Result.success((Student)jedisClusterClient.getObject(“student”)); } @PostMapping(“/testT”) public Result<Student> testT(@RequestBody Student t){ jedisClusterClient.setObject(“student”, t, 0); return Result.success(jedisClusterClient.getT(“student”)); } @PostMapping(“/testT1”) public Result<Person> testT1(@RequestBody Person t){ jedisClusterClient.setT(“person”, t, 0); return Result.success(jedisClusterClient.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){ jedisClusterClient.set(UserKey.getById, “map”,t); log.info(“键值是否存在:{}”,jedisClusterClient.exists(UserKey.getById, “map”)); redisService.getResult(); return Result.success(jedisClusterClient.get(UserKey.getById, “map”,Map.class)); } } |
6,测试结果
还没有评论,来说两句吧...