兼容集群与单机redis的java客房端
1.maven依赖
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> <exclusions> <exclusion> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.4.2</version> </dependency>
2.Service类
package com.xx.service; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCommands; import javax.annotation.PostConstruct; import java.util.HashSet; import java.util.Set; @Service public class RedisService { @Value("${redis.hosts}") private String hosts; @Value("${redis.cluster}") private Boolean cluster; private JedisCommands jedisCommands; @PostConstruct public void init(){ if(cluster) { Set<HostAndPort> hostSet = new HashSet<HostAndPort>(); String[] hostPort = hosts.split(";"); for (String hp : hostPort) { String hpa[] = hp.split(":"); hostSet.add(new HostAndPort(hpa[0], Integer.valueOf(hpa[1]))); } jedisCommands = new JedisCluster(hostSet, 2000, 200, 3, new GenericObjectPoolConfig()); }else{ String[] ipPort=hosts.split(":"); jedisCommands= new Jedis(ipPort[0], Integer.valueOf(ipPort[1]), false); } } public void set(String key,String val,long timeout){ boolean keyExist = jedisCommands.exists(key); // NX是不存在时才set, XX是存在时才set, EX是秒,PX是毫秒 if (keyExist) { jedisCommands.del(key); } jedisCommands.set(key, val, "NX", "PX", timeout); } public String get(String key){ return jedisCommands.get(key); } }3.springboot配置
redis: hosts: xx:6379 cluster: false
4.Application.java调用
ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args); RedisService fs=applicationContext.getBean(RedisService.class); fs.set("test","12",100000); System.out.println(fs.get("test"));
redis的scan方法,无法遍历所有key,如下:
public static void main(String args[]){ Jedis jc= new Jedis("172.21.xx.xx",1110, false); scan(jc,"*"); } public static void scan(Jedis jedis,String key){ // 游标初始值为0 String cursor = ScanParams.SCAN_POINTER_START; ScanParams scanParams = new ScanParams(); scanParams.match(key);// 匹配以 PLFX-ZZSFP-* 为前缀的 key scanParams.count(10); while (true){ Calendar cal= Calendar.getInstance(); for(int i=1;i<11;i++){ jedis.set(cal.getTimeInMillis()+String.valueOf(i),String.valueOf(i)); } //使用scan命令获取数据,使用cursor游标记录位置,下次循环使用 ScanResult<String> scanResult = jedis.scan(cursor, scanParams); cursor = scanResult.getStringCursor();// 返回0 说明遍历完成 List<String> list = scanResult.getResult(); long t1 = System.currentTimeMillis(); for(int m = 0;m < list.size();m++){ String mapentry = list.get(m); log.info(mapentry); } long t2 = System.currentTimeMillis(); log.info("获取" + list.size() + "条数据,耗时: " + (t2-t1) + "毫秒,cursor:" + cursor); if ("0".equals(cursor)){ break; } } }
相关阅读
评论:
↓ 广告开始-头部带绿为生活 ↓
↑ 广告结束-尾部支持多点击 ↑