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)
- 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)
- 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<!--Redis ---
- 2<dependency>
- 3<groupId>redis.clients</groupId>
- 4<artifactid>jedis</artifactid>
- 5<version>2.0.0</version>
- 6<type>jar</type>
- 7<scope>compile</scope>
- 8</dependency>
Test Example Original: http://flychao88.iteye.com/blog/1527163
- Package com.lujianing.utils;
- Import Org.junit.Before;
- Import Org.junit.Test;
- Import Redis.clients.jedis.Jedis;
- Import Redis.clients.jedis.JedisPool;
- Import Redis.clients.jedis.JedisPoolConfig;
- Import Java.util.HashMap;
- Import Java.util.Iterator;
- Import java.util.List;
- Import Java.util.Map;
- /**
- * Created by lujianing on 14-2-28.
- */
- public class Jedisutiltest {
- Jedispool Pool;
- Jedis Jedis;
- @Before
- public void SetUp () {
- Pool = New Jedispool (New Jedispoolconfig (), "192.168.10.61");
- Jedis = Pool.getresource ();
- Jedis.auth ("password");
- }
- @Test
- public void Testget () {
- System.out.println (Jedis.get ("Lu"));
- }
- /**
- * Redis Storage of primary strings
- * CRUD
- */
- @Test
- public void testbasicstring () {
- -----Add Data----------
- Jedis.set ("name", "MINXR");//Addvalue-->minxr to key-->name
- System.out.println (Jedis.get ("name"));//Execution Result: MINXR
- -----Modify the Data-----------
- 1, modified on the original basis
- Jedis.append ("name", "Jarorwar"); Very intuitive, like the map will Jarorwar append to the already existing value
- System.out.println (Jedis.get ("name"));//Execution Result: Minxrjarorwar
- 2, directly overwrite the original data
- Jedis.set ("name", "Min Xiaorong");
- System.out.println (Jedis.get ("name"));//Execution Result: Min Xiaorong
- Delete the record for key
- Jedis.del ("name");
- System.out.println (Jedis.get ("name"));//execution result: null
- /**
- * Mset equivalent to
- * Jedis.set ("name", "MINXR");
- * Jedis.set ("Jarorwar", "Min Xiaorong");
- */
- Jedis.mset ("name", "MINXR", "Jarorwar", "Min Xiaorong");
- System.out.println (Jedis.mget ("name", "Jarorwar"));
- }
- /**
- * Jedis Operation Map
- */
- @Test
- public void TestMap () {
- Map<string,string> user=new HashMap<string,string> ();
- User.put ("name", "MINXR");
- User.put ("pwd", "password");
- Jedis.hmset ("user", user);
- Remove the name from the user and execute the result: [minxr]--> Note The result is a generic list
- 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
- List<String> rsmap = jedis.hmget ("User", "name");
- System.out.println (RSMAP);
- Delete a key value from a map
- Jedis.hdel ("User", "pwd");
- System.out.println (Jedis.hmget ("User", "pwd")); Because it was deleted, NULL is returned.
- System.out.println (Jedis.hlen ("user")); Returns the number of values stored in the key for user 1
- System.out.println (jedis.exists ("user"));//Whether there is a record of key for user returns true
- System.out.println (Jedis.hkeys ("user"));//returns all keys in the Map object [pwd, name]
- System.out.println (jedis.hvals ("user"));//Returns all value in the Map object [MINXR, password]
- Iterator<String> iter=jedis.hkeys ("user"). Iterator ();
- while (Iter.hasnext ()) {
- String key = iter.next (); System.out.println (key+ ":" +jedis.hmget ("User", key));
- }
- }
- /**
- * Jedis Operation List
- */
- @Test
- public void Testlist () {
- Remove all content before starting
- Jedis.del ("Java framework");
- System.out.println (Jedis.lrange ("Java framework", 0,-1));
- Store three data in the key Java framework first
- Jedis.lpush ("Java framework", "Spring");
- Jedis.lpush ("Java framework", "struts");
- Jedis.lpush ("Java framework", "Hibernate");
- And then take out all the data jedis.lrange is out by range,
- 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
- System.out.println (Jedis.lrange ("Java framework", 0,-1));
- }
- /**
- * Jedis Operation Set
- */
- @Test
- public void Testset () {
- Add to
- Jedis.sadd ("Sname", "MINXR");
- Jedis.sadd ("Sname", "Jarorwar");
- Jedis.sadd ("Sname", "Min Xiaorong");
- Jedis.sadd ("Sanme", "Noname");
- Remove Noname
- Jedis.srem ("Sname", "Noname");
- System.out.println (Jedis.smembers ("sname"));//Get all added value
- System.out.println (Jedis.sismember ("sname", "MINXR"));//Determine if MINXR is an element of the Sname collection
- System.out.println (Jedis.srandmember ("sname"));
- System.out.println (Jedis.scard ("sname"));//Returns the number of elements of the collection
- }
- @Test
- public void Test () throws Interruptedexception {
- You can use wildcard characters to pass in keys.
- System.out.println (Jedis.keys ("*")); Returns all keys in the current library [Sose, Sanme, name, Jarorwar, foo, sname, Java Framework, user, Braand]
- System.out.println (Jedis.keys ("*name"));//Return sname [sname, name]
- System.out.println (Jedis.del ("Sanmdde"));//delete key for Sanmdde object Delete successfully returned 1 delete failed (or does not exist) return 0
- System.out.println (Jedis.ttl ("sname"));//Returns the valid time of the given key, if 1 means that it is always valid
- Jedis.setex ("TimeKey", "min");//This method allows you to specify the survival (active time) time of the key as seconds
- Thread.Sleep (5000);//Sleep 5 seconds later, the remaining time will be <=5
- System.out.println (Jedis.ttl ("TimeKey")); Output is 5
- Jedis.setex ("TimeKey", 1, "min"); After setting it to 1, let's see the remaining time is 1.
- System.out.println (Jedis.ttl ("TimeKey")); Output is 1
- System.out.println (jedis.exists ("key"));//check if key exists System.out.println (Jedis.rename ("TimeKey", "Time");
- System.out.println (Jedis.get ("TimeKey"));//NULL is returned as a result of removal
- System.out.println (Jedis.get ("Time")); The value min can be obtained because the TimeKey is renamed to TIME
- Jedis sort
- Note that the Rpush and Lpush here are the operations of the list. is a doubly linked list (but from a performance perspective)
- Jedis.del ("a");//clear the data before adding data to test
- Jedis.rpush ("A", "1");
- Jedis.lpush ("A", "6");
- Jedis.lpush ("A", "3");
- Jedis.lpush ("A", "9");
- System.out.println (Jedis.lrange ("a", 0,-1);//[9, 3, 6, 1]
- System.out.println (Jedis.sort ("a")); [1, 3, 6, 9]//input sort results
- System.out.println (Jedis.lrange ("a", 0,-1));
- }
- }
Redis will save data to the hard disk on a regular basis
Installation of Redis under Windows is used