Installation of Redis under Windows is used

Source: Internet
Author: User
Tags delete key

Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set-ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to the disk or writes the modified operation to the appended record file, and implements the Master-slave on this basis.

Objective

Because it is the first use, so it is installed and used under Windows, referring to a few blogs, the following:

Installing Redis

Official website: http://redis.io/

Official download: Http://redis.io/download can download different versions as needed

Windows Edition: Https://github.com/mythz/redis-windows

GitHub resources can be downloaded directly from the zip (this is for the students who do not know the friendship tips).

After the download is complete, you can right-click on a hard drive such as D:\Redis\redis-2.6.

Under D:\Redis\redis-2.6\bin\release There are two zip packages, one 32-bit and one 64-bit.

Extract the D:\Redis\redis-2.6 root directory according to the number of bits in your windows.

2. Start Redis

Turn on the service after entering the Redis directory (note plus redis.conf)

    1. Redis-server.exe redis.conf

The Redis service shuts down automatically when this window is to be turned on and off

Redis will automatically save the data to the hard drive so the picture is that I have one more DB loaded from disk when I start the second time.

3. Test use

Also open a command line window into the Redis directory (note to modify your own IP)

    1. Redis-cli.exe-h 192.168.10.61-p 6379

4.Java Development Kit Jedis

Jedis:http://www.oschina.net/p/jedis (Redis's official Preferred Java Development Kit)

  1. 1<!--Redis ---
  2. 2<dependency>
  3. 3<groupId>redis.clients</groupId>
  4. 4<artifactid>jedis</artifactid>
  5. 5<version>2.0.0</version>
  6. 6<type>jar</type>
  7. 7<scope>compile</scope>
  8. 8</dependency>

Test Example Original: http://flychao88.iteye.com/blog/1527163

  1. Package com.lujianing.utils;
  2. Import Org.junit.Before;
  3. Import Org.junit.Test;
  4. Import Redis.clients.jedis.Jedis;
  5. Import Redis.clients.jedis.JedisPool;
  6. Import Redis.clients.jedis.JedisPoolConfig;
  7. Import Java.util.HashMap;
  8. Import Java.util.Iterator;
  9. Import java.util.List;
  10. Import Java.util.Map;
  11. /**
  12. * Created by lujianing on 14-2-28.
  13. */
  14. public class Jedisutiltest {
  15. Jedispool Pool;
  16. Jedis Jedis;
  17. @Before
  18. public void SetUp () {
  19. Pool = New Jedispool (New Jedispoolconfig (), "192.168.10.61");
  20. Jedis = Pool.getresource ();
  21. Jedis.auth ("password");
  22. }
  23. @Test
  24. public void Testget () {
  25. System.out.println (Jedis.get ("Lu"));
  26. }
  27. /**
  28. * Redis Storage of primary strings
  29. * CRUD
  30. */
  31. @Test
  32. public void testbasicstring () {
  33. -----Add Data----------
  34. Jedis.set ("name", "MINXR");//Addvalue-->minxr to key-->name
  35. System.out.println (Jedis.get ("name"));//Execution Result: MINXR
  36. -----Modify the Data-----------
  37. 1, modified on the original basis
  38. Jedis.append ("name", "Jarorwar"); Very intuitive, like the map will Jarorwar append to the already existing value
  39. System.out.println (Jedis.get ("name"));//Execution Result: Minxrjarorwar
  40. 2, directly overwrite the original data
  41. Jedis.set ("name", "Min Xiaorong");
  42. System.out.println (Jedis.get ("name"));//Execution Result: Min Xiaorong
  43. Delete the record for key
  44. Jedis.del ("name");
  45. System.out.println (Jedis.get ("name"));//execution result: null
  46. /**
  47. * Mset equivalent to
  48. * Jedis.set ("name", "MINXR");
  49. * Jedis.set ("Jarorwar", "Min Xiaorong");
  50. */
  51. Jedis.mset ("name", "MINXR", "Jarorwar", "Min Xiaorong");
  52. System.out.println (Jedis.mget ("name", "Jarorwar"));
  53. }
  54. /**
  55. * Jedis Operation Map
  56. */
  57. @Test
  58. public void TestMap () {
  59. Map<string,string> user=new HashMap<string,string> ();
  60. User.put ("name", "MINXR");
  61. User.put ("pwd", "password");
  62. Jedis.hmset ("user", user);
  63. Remove the name from the user and execute the result: [minxr]--> Note The result is a generic list
  64. The first parameter is the key to the Map object in Redis, followed by the key of the object placed in the map, followed by the key can be more than a variable parameter
  65. List<String> rsmap = jedis.hmget ("User", "name");
  66. System.out.println (RSMAP);
  67. Delete a key value from a map
  68. Jedis.hdel ("User", "pwd");
  69. System.out.println (Jedis.hmget ("User", "pwd")); Because it was deleted, NULL is returned.
  70. System.out.println (Jedis.hlen ("user")); Returns the number of values stored in the key for user 1
  71. System.out.println (jedis.exists ("user"));//Whether there is a record of key for user returns true
  72. System.out.println (Jedis.hkeys ("user"));//returns all keys in the Map object [pwd, name]
  73. System.out.println (jedis.hvals ("user"));//Returns all value in the Map object [MINXR, password]
  74. Iterator<String> iter=jedis.hkeys ("user"). Iterator ();
  75. while (Iter.hasnext ()) {
  76. String key = iter.next (); System.out.println (key+ ":" +jedis.hmget ("User", key));
  77. }
  78. }
  79. /**
  80. * Jedis Operation List
  81. */
  82. @Test
  83. public void Testlist () {
  84. Remove all content before starting
  85. Jedis.del ("Java framework");
  86. System.out.println (Jedis.lrange ("Java framework", 0,-1));
  87. Store three data in the key Java framework first
  88. Jedis.lpush ("Java framework", "Spring");
  89. Jedis.lpush ("Java framework", "struts");
  90. Jedis.lpush ("Java framework", "Hibernate");
  91. And then take out all the data jedis.lrange is out by range,
  92. The first one is key, the second is the starting position, the third is the end position, Jedis.llen gets the length-1 means get all
  93. System.out.println (Jedis.lrange ("Java framework", 0,-1));
  94. }
  95. /**
  96. * Jedis Operation Set
  97. */
  98. @Test
  99. public void Testset () {
  100. Add to
  101. Jedis.sadd ("Sname", "MINXR");
  102. Jedis.sadd ("Sname", "Jarorwar");
  103. Jedis.sadd ("Sname", "Min Xiaorong");
  104. Jedis.sadd ("Sanme", "Noname");
  105. Remove Noname
  106. Jedis.srem ("Sname", "Noname");
  107. System.out.println (Jedis.smembers ("sname"));//Get all added value
  108. System.out.println (Jedis.sismember ("sname", "MINXR"));//Determine if MINXR is an element of the Sname collection
  109. System.out.println (Jedis.srandmember ("sname"));
  110. System.out.println (Jedis.scard ("sname"));//Returns the number of elements of the collection
  111. }
  112. @Test
  113. public void Test () throws Interruptedexception {
  114. You can use wildcard characters to pass in keys.
  115. System.out.println (Jedis.keys ("*")); Returns all keys in the current library [Sose, Sanme, name, Jarorwar, foo, sname, Java Framework, user, Braand]
  116. System.out.println (Jedis.keys ("*name"));//Return sname [sname, name]
  117. System.out.println (Jedis.del ("Sanmdde"));//delete key for Sanmdde object Delete successfully returned 1 delete failed (or does not exist) return 0
  118. System.out.println (Jedis.ttl ("sname"));//Returns the valid time of the given key, if 1 means that it is always valid
  119. Jedis.setex ("TimeKey", "min");//This method allows you to specify the survival (active time) time of the key as seconds
  120. Thread.Sleep (5000);//Sleep 5 seconds later, the remaining time will be <=5
  121. System.out.println (Jedis.ttl ("TimeKey")); Output is 5
  122. Jedis.setex ("TimeKey", 1, "min"); After setting it to 1, let's see the remaining time is 1.
  123. System.out.println (Jedis.ttl ("TimeKey")); Output is 1
  124. System.out.println (jedis.exists ("key"));//check if key exists System.out.println (Jedis.rename ("TimeKey", "Time");
  125. System.out.println (Jedis.get ("TimeKey"));//NULL is returned as a result of removal
  126. System.out.println (Jedis.get ("Time")); The value min can be obtained because the TimeKey is renamed to TIME
  127. Jedis sort
  128. Note that the Rpush and Lpush here are the operations of the list. is a doubly linked list (but from a performance perspective)
  129. Jedis.del ("a");//clear the data before adding data to test
  130. Jedis.rpush ("A", "1");
  131. Jedis.lpush ("A", "6");
  132. Jedis.lpush ("A", "3");
  133. Jedis.lpush ("A", "9");
  134. System.out.println (Jedis.lrange ("a", 0,-1);//[9, 3, 6, 1]
  135. System.out.println (Jedis.sort ("a")); [1, 3, 6, 9]//input sort results
  136. System.out.println (Jedis.lrange ("a", 0,-1));
  137. }
  138. }

Redis will save data to the hard disk on a regular basis

Installation of Redis under Windows is used

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.