一、利用jedis客户端直接操作reids
基础操作参考
1.1 概念: 切片池 & 非切片池
关于切片池和非切片池的区别, 一般项目基本都使用非切片池;切片池主要用于分布式项目,可以设置主从Redis库。
1.2 获取redis客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| public class RedisClient {
private Jedis jedis;
private JedisPool jedisPool;
private ShardedJedis shardedJedis;
private ShardedJedisPool shardedJedisPool;
private void initialPool(){ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(20); config.setMaxIdle(5); config.setMaxWait(10001); config.setTestOnBorrow(false);
jedisPool = new JedisPool(config, "127.0.0.1", 6379); }
private void initialShardedPool(){ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(20); config.setMaxIdle(5); config.setMaxWait(10001); config.setTestOnBorrow(false); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(new JedisShardInfo("127.0.0.1", 6379, "master"));
shardedJedisPool = new ShardedJedisPool(config, shards); }
public RedisClient(){ initialPool(); initialShardedPool(); shardedJedis = shardedJedisPool.getResource(); jedis = jedisPool.getResource(); }
public Jedis getJedis() { return jedis; }
public void setJedis(Jedis jedis) { this.jedis = jedis; }
public ShardedJedis getShardedJedis() { return shardedJedis; }
public void setShardedJedis(ShardedJedis shardedJedis) { this.shardedJedis = shardedJedis; } }
|
1.3 key操作和String操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| public class RedisTest {
private RedisClient redisClient = new RedisClient(); private Jedis jedis; private ShardedJedis shardedJedis;
@Before public void getCli(){ jedis = redisClient.getJedis(); shardedJedis = redisClient.getShardedJedis(); }
@Test public void KeyOperate(){ System.out.println("===key===");
System.out.println("清空库中所有数据:" + jedis.flushDB());
System.out.println("判断key999键是否存在:" + shardedJedis.exists("key999")); System.out.println("新增key001及其对应值:" + shardedJedis.set("key001", "value001")); System.out.println("判断key001键是否存在:" + shardedJedis.exists("key001"));
System.out.println("新增key002键值:" + shardedJedis.set("key002", "value002")); System.out.println("系统中所有键如下:"); Set<String> keys = jedis.keys("*"); Iterator<String> it = keys.iterator(); while (it.hasNext()) { String key = it.next(); System.out.println(key); }
System.out.println("系统中删除key002:" + jedis.del("key002")); System.out.println("判断key002是否存在:" + shardedJedis.exists("key002"));
System.out.println("设置key001的过期时间为5秒" + jedis.expire("key001", 5)); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("查看key001的剩余生存时间:"+jedis.ttl("key001")); System.out.println("移除key001的生存时间:"+jedis.persist("key001")); System.out.println("查看key001的剩余生存时间:"+jedis.ttl("key001"));
System.out.println("查看key所存储的值的类型" + jedis.type("key001"));
}
@Test public void StringOperate(){ System.out.println("====String Operate===="); System.out.println("清空所有数据:"+jedis.flushDB());
System.out.println("=====增加====="); jedis.set("key001", "value001"); jedis.set("key002", "value002"); jedis.set("key003", "value003"); System.out.println("已新增的3个键值如下:"); System.out.println(jedis.get("key001")); System.out.println(jedis.get("key002")); System.out.println(jedis.get("key003"));
System.out.println("=====删除====="); System.out.println("删除key003对应的键值:" + jedis.del("key003")); System.out.println("获取key003对应的值:" + jedis.get("key003"));
System.out.println("=======修改======"); System.out.println("直接覆盖key001数据"+jedis.set("key001", "value001-update")); System.out.println("获取key001对应的值:" + jedis.get("key001")); System.out.println("在key002后面追加"+jedis.append("key002","-append2")); System.out.println("获取key002对应的值:" + jedis.get("key002"));
} }
|
二、PubSub机制
参考链接
三、从缓存中处理请求的两种方式:time&count
首先:两种模式下,时间阈值(假设10s) 和 count阈值(100) 都是有用的:
- 时间模式:若5s时已经add了100个,再等5s到10s后处理;若到了10s,还没add到100个,也处理
- cout模式(适合较多数据的时候):若5s时add到了100,直接处理;若到了10s,还没add到100个,也处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| if (MODE_TIME.equals(mode)) { do { if (buffer.size() < countThreshold) { C item = pool.outPool(); if (item != null) { buffer.add(item); } } } while (System.currentTimeMillis() < end); } else if (MODE_COUNT.equals(mode)) { do { C item = pool.outPool(); if (item != null) { buffer.add(item); } } while (System.currentTimeMillis() < end && buffer.size() < countThreshold); pool.releaseLock(); }
|
四、Redis Sentinel
官方文档
Redis Sentinel机制与用法