配置方法git
默认值redis
动态配置服务器
未使用pipeline的批量网络通讯模型网络
假设客户端在上海,Redis服务器在北京。相距1300千米。假设光纤速度≈光速2/3,即30000千米/秒2/3。那么一次命令的执行时间就是(13002)/(300002/3)=13毫秒。Redis万级QPS,一次命令执行时间只有0.1毫秒,所以网络传输消耗13毫秒是不能接受的。在N次命令操做下,Redis的使用效率就不是很高了。运维
使用pipeline的网络通讯模型maven
引入maven依赖工具
<dependency> <groupId>redis.clients</groupId> <artifacId>jedis</artifacId> <version>2.9.0</version> <type>jar</type> <scope>compile</scope> </dependency>
// 不用pipeline Jedis jedis = new Jedis("127.0.0.1", 6379); for (int i = 0; i < 10000; i++) { jedis.hset("hashkey" + i, "field" + i, "value=" + i); }
不用pipeline,10000次hset,总共耗时50s,不一样网络环境可能有所不一样spa
// 使用pipeline, 咱们将10000条命令分100次pipeline,每次100条命令 Jedis jedis = new Jedis("127.0.0.1", 6379); for (int i = 0; i < 100; i++) { Pipeline pipeline = jedis.pipeline(); for (int j = i * 100; j < (i + 1) * 100; j++) { pipeline.hset("hashkey:" + j, "field" + j, "value" + j); } pipeline.syncAndReturnAll(); }
使用pipelne,10000次hset,总共耗时0.7s,不一样网络环境可能有所不一样。code
可见在执行批量命令时,使用pipeline对Redis的使用效率提高是很是明显的。生命周期
mset、mget等操做是原子性操做,一次m操做只返回一次结果。
pipeline非原子性操做,只是将N次命令打个包传输,最终命令会被逐条执行,客户端接收N次返回结果。
订阅者能够订阅多个频道
发布
API:publish channel message
redis> publish sohu:tv "hello world" (integer) 3 #订阅者个数 redis> publish sohu:auto "taxi" (integer) #没有订阅者
订阅
API:subscribe [channel] #一个或多个
redis> subscribe sohu:tv 1) "subscribe" 2) "sohu:tv" 3) (integer) 1 1) "message" 2) "sohu:tv" 3) "hello world"
取消订阅
API:unsubscribe [channel] #一个或多个
redis> unsubscribe sohu:tv 1) "unsubscribe" 2) "sohu:tv" 3) (integer) 0
其余API
消息队列模型
发布订阅模型,订阅者均能收到消息。消息队列,只有一个订阅者能收到消息。所以使用发布订阅仍是消息队列,要搞清楚使用场景。
GEO:存储经纬度,计算两地距离,范围计算等。
API:geoadd key longitude latitude member # 增长地理位置信息
redis> geoadd cities:locations 116.28 39.55 bejing (integer) 1 redis> geoadd cities:locations 117.12 39.08 tianjin 114.29 38.02 shijiazhuang 118.01 39.38 tangshan 115.29 38.51 baoding (integer) 4
API:geopos key member [member...] # 获取地理位置信息
redis> geopos cities:locations tianjin 1)1) "117.12000042200088501" 2) "39.0800000535766543"
API:geodist key member1 member2 [unit] # 获取两位置距离,unit:m(米)、km(公里)、mi(英里)、ft(尺)
reids> geodist cities:locations tianjin beijing km "89.2061"
API:
获取指定位置范围内的地理位置信息集合
georadius key longitude latitude radius m|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key]
georadiusbymember key member radius m|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key]
withcoord: 返回结果中包含经纬度
withdist: 返回结果中包含距离中心节点的距离
withhash: 返回结果中包含geohash
COUNT count:指定返回结果的数量
asc|desc:返回结果按照距离中心节点的距离作升序/降序
store key:将返回结果的地理位置信息保存到指定键
storedist key:将返回结果距离中心点的距离保存到指定键
redis> georadiusbymember cities:locations beijing 150 km 1)"beijing" 2) "tianjin" 3) "tangshan" 4) "baoding"