redis of five data types and scenarios

Foreword

redis is in the form of key-value pairs to store data, key type can only be a String, but the value type can have String, List, Hash, Set, Sorted Set five kinds to meet the specific needs of different scenarios.

The examples in this blog are not the console as a client redis, but will be used in the java redis test

Java redis requires a driver package, by introducing the maven can depend

        <dependency>
            <groupId>org.rarefiedredis.redis</groupId>
            <artifactId>redis-java</artifactId>
            <version>0.0.17</version>
        </dependency>

 

String

String type is the most basic kind of key-value store form, value can be a fact, not only String, can also be a numeric type. Such is often used as a counter increment-decrement features, the number of fans can be used, the number of micro-blog like.

Examples

1          // Redis connection to the local service 
2          Jedis jedis = new new Jedis ( "localhost" );
 3          System.out.println ( "successfully connected" );
 4          // Check whether the service is running 
5          System.out.println ( "service is run: "+ jedis.ping ());
 . 6          // String example 
. 7          jedis.set (" Hello ", String.valueOf (. 1 ));
 . 8          jedis.incr (" Hello " );
 . 9          jedis.set (" hello1 "," word1 " );
 10          System.out.println (jedis.get (" Hello " ));
 . 11         System.out.println(jedis.mget("hello","hello1"));

Common Commands

  • set
  • get
  • mget
  • incr
  • decr

 

List

list is the list, in redis implemented as a doubly linked list can be inserted from both sides, delete data. Scenarios can watchlist microblogging, fan list, message list and so on.

There is a lrange function, you can start to read how many elements from an element, can be used to implement paging functionality.

Examples

. 1          / * List example, two-way linked list structure, suitable for the message queue,
 2          but in fact the real applications usually made with a special message queue middleware e.g. RabbitMQ * / 
. 3          jedis.lpush ( "dormitory 201", "HLF" );
 . 4          jedis.lpush ( "dormitory 201", "CSS" );
 . 5          jedis.lpush ( "dormitory 201", "TY" );
 . 6          jedis.lpush ( "dormitory 201", "JY" );
 . 7          List <String> name = jedis.lrange ( "201 quarters", 0,3 );
 . 8          for (String Person: name
 . 9               ) {
 10              of System.out.print (Person + "" );
 . 11          }

 

Common Commands

  •  lpush
  • Rpus
  • lpush
  • lpop
  • lrange

 

Hash

hash value is stored in a key type form, suitable for storing the object type information, such as personal information, merchandise information and the like.

Examples

 1         //hash实例,适合存储对象
 2         HashMap<String,String> map = new HashMap<String, String>();
 3         map.put("name","hlf");
 4         map.put("sex","女");
 5         map.put("age","21");
 6         jedis.hmset("hlf",map);
 7         jedis.hset("hlf","major","software");
 8         Map<String,String> map1 = jedis.hgetAll("hlf");
 9         String age = jedis.hget("hlf","age");
10         System.out.println(map1);
11         System.out.println(age);

 

Common Commands

  • hset
  • hmset
  • hget
  • hgetAll

 

Set

represents a set of storage elements do not coincide collection, a collection of support as set seizing, and set operations, and therefore suitable for common functions such as Friends

Examples

1         //set实例
2         jedis.sadd("set","hhh");
3         jedis.sadd("set","ff");
4         jedis.sadd("set","hhh");
5         System.out.println(jedis.smembers("set"));
6         jedis.sadd("set1","oo");
7         jedis.sadd("set1","ff");
8         System.out.println("交集:"+jedis.sinter("set","set1"));
9         System.out.println("合集:"+jedis.sunion("set","set1"));

 

Common Commands

  • sadd
  • spop
  • smembers
  • sunion
  • sinter

 

Sorted Set

With respect to Set, Sorted Set Score as a plurality of weights, so that the set of elements which can be sorted according to the score, it is noted Set, so that the elements can not be overlapped inside

Examples

        //sorted set实例
        jedis.zadd("set2",4,"redis");
        jedis.zadd("set2",3,"mysql");
        jedis.zadd("set2",2,"kk");
        jedis.zadd("set2",1,"redis");
        System.out.println(jedis.zrangeByScore("set2",0,4));

 

Common Commands

  • zadd
  • Pop
  • zrangeByScore

 

Guess you like

Origin www.cnblogs.com/huanglf714/p/11082992.html