Redis in Action(一):基本语法

1,redis是一非关系型数据库,key-value,高可用(performance),支持内存持久化到磁盘,主从复制,发布订阅;

2,redis 支持5中数据结构:Strings,Lists,Sets,Hashes and Zsets

String: Strings,intergers,or floating-point values

List: Linked list of Strings

Set:Unorderd collection of unique Strings

Hash: unordered hash table of keys to values

Zset(sorted set): Ordered mapping of string members to floating-point scores,ordered by score

3,redis-cli.exe中基本操作;

> set a 11

>get a 

>del a

(1) List 操作:

add操作:rpush listname value,lpush listname value(其中 r 和 l 表示从左边还是右边加入)

列出全部数据:lrange list-key 0 -1

ltrim修剪,类似substring():ltrim listkey 2 -1;  从第三位开支知道最后

(2) set操作:

sadd setname value

列出全部数据

smember set-key

(3) hashs 操作:

put操作: hset hash-key key value

打印所有:hgetall hash-key;

删除一个:hdel hash-key  key

(4) 排序 zadd zrange,zrangebyscore zrem

添加:zadd zset-key score key;

按照score 来进行排序:zrange zset-key 0 -1 withscores

(5) hmset:用于同时存放多个filed(key,value)进入hash中;

4,redis 发布\订阅模式(pub\sub):

   JedisPool jedisPool = new JedisPool(new JedisPoolConfig(),ip,port);

   Jedis jedis = jedisPool.getResource();

   String channelName = "redisChannel";

   class Subcriber extends JedisPubSub{
        public void onMessage(String channel,String message){
        }
    } 

  创建发布线程和订阅线程,分别做一下的事情

  jedis.publish(channel,content);//发布

  jedis.subscribe(subcriber,channel);//订阅

在使用redis的发布和订阅的时候,有两个问题:

(a)当数据过大,redis 的吞吐量可能不足以keep a large outgoing buffer,数据过大可能导致redis 变的非常慢,甚至是crash,系统会自动杀掉redis(在window下  启动了100个线程一直发送数据,100个线程监听,cpu是100%)

(b)监听redis的服务器,可能在某个时候连接断开,jedis能够在动重连,但是在连接的过程中,可能redis上向subcriber已经发送的了数据了,但是接受方进程没有接受到消息,导致消息丢失;

5,设置 transaction(事物)

Pipline pip = jedis.piplined();

pip.zadd(...)

6,设置key过期时间

jedis.expire(key,time)

jedis.ttl(key)//剩余多少时间

猜你喜欢

转载自blog.csdn.net/waterflying2015/article/details/81176764
今日推荐