0. Installation and testing
- Installing Redis on Windows
Redis 's official website is Redis.io, the installation of Redis to Windows, you can unzip the attachment redis.zip to any local path (such as: e-disk).
- Start Redis Server Side
Use the Cmd.exe tool to enter the unzipped Redis folder and enter the following command to open the Redis server side using the parameters specified in configuration file redis.windows.conf
E:\redis>redis-server.exe redis.windows.conf[3136] 15:30:18.916 # Creating Server TCP listening socket 127.0.0.1:6379: Bind:no Error
2. Test the installation successfully with Javamaven Pom File introduction dependency:
<!--Jedis--><dependency> <groupId>redis.clients</groupId> <artifactId> jedis</artifactid> <version>2.9.0</version> <type>jar</type> < Scope>compile</scope></dependency>
Test Code:
Package Com.wenniuwuren.redis;import Redis.clients.jedis.jedis;public class Jedisutil {public static void main ( String[] args) { Jedis Jedis = new Jedis ("localhost"); Jedis.set ("foo", "Bar"); String value = Jedis.get ("foo"); System.out.println ("value=" + value);} }
test Result: Value=bar
A. Redis Java string (string) instance
Insert a key/value into the Redis server, print the value result, and then delete
Jedis Jedis = new Jedis ("localhost"), Jedis.set ("foo", "Bar"); String value = Jedis.get ("foo"); System.out.println ("value=" + value); Jedis.del ("foo"); System.out.println ("value=" + jedis.get ("foo"));
Test Results:Value=barValue=null
two. Redis Java list (list) instance
public static void Main (string[] args) { Jedis Jedis = new Jedis ("localhost"); Jedis.set ("foo", "Bar"); String value = Jedis.get ("foo"); System.out.println ("value=" + value); Jedis.del ("foo"); System.out.println ("value=" + jedis.get ("foo")); Store data to list Jedis.lpush ("Tutorial-list", "Redis"); Jedis.lpush ("Tutorial-list", "Mongodb"); Jedis.lpush ("Tutorial-list", "Mysql"); Jedis.rpush ("Tutorial-list", "Memcached"); Output list data list<string> list = Jedis.lrange ("Tutorial-list", 0, +); for (int i=0; I<list.size (), i++) { System.out.println ("Stored string in Redis::" +list.get (i));} }
Test Results:Value=barValue=nullStored string in Redis:: MysqlStored string in Redis:: MongodbStored string in Redis:: RedisStored string in Redis:: Memcached
three. Redis Java set (collection) instance
public static void Main (string[] args) { Jedis Jedis = new Jedis ("localhost"); Adds the given element to the collection Jedis.sadd ("Settest", "a"); Jedis.sadd ("Settest", "B"); Jedis.sadd ("Settest", "a"); Checks if the given element exists in the collection System.out.println ("A is exist in settest?" + Jedis.sismember ("Settest", "a")); Set set = Jedis.smembers ("Settest"); Returns all the elements contained in the collection Iterator Iterator = Set.iterator (); while (Iterator.hasnext ()) { System.out.println (Iterator.next ()); } If the given element exists in the collection, then the element Jedis.srem ("Settest", "a") is removed; Set Finalset = jedis.smembers ("Settest"); Iterator Iterator1 = Finalset.iterator (); System.out.println ("After deleting elements in collection ..."); while (Iterator1.hasnext ()) { System.out.println (Iterator1.next ());} }
Execution Result:A is exist in Settest? TrueabDelete the elements in the collection after ...b
four. Redis Java hash (hash) instance
public static void Main (string[] args) { Jedis Jedis = new Jedis ("localhost"); Set the key value pair Jedis.hset ("Hashtest", "a", "B"); Jedis.hset ("Hashtest", "B", "C"); Jedis.hset ("Hashtest", "a", "B"); Gets all the key values in the hash to map map = Jedis.hgetall ("Hashtest"); Gets the value System.out.println (Map.get ("a")) based on key; System.out.println (Map.get ("B")); System.out.println (Map.get ("C")); System.out.println ("after delete ..."); Jedis.hdel ("Hashtest", "a"); Map Map1 = Jedis.hgetall ("Hashtest"); System.out.println (Map1.get ("a")); System.out.println (Map1.get ("B")); System.out.println (Map1.get ("C"));}
Test Results:bCNULLDelete After ...NULLCNULL
Five. Redis Java Zset (ordered collection) instances
public static void Main (string[] args) { Jedis Jedis = new Jedis ("localhost"); Adds a member with a given score to an ordered set of Jedis.zadd ("Zsettest", Ten, "a"); Jedis.zadd ("Zsettest", "B"); Gets all elements of an ordered collection within a given score range Set zset = Jedis.zrangebyscore ("Zsettest", 5, ten); Iterator Iterator = Zset.iterator (); while (Iterator.hasnext ()) { System.out.println (Iterator.next ()); } System.out.println ("..."); The Set zset1 = Jedis.zrange ("Zsettest", 0,-1) for all elements of an ordered set within a given position, based on the sorting order of the scores; Iterator Iterator1 = Zset1.iterator (); while (Iterator1.hasnext ()) { System.out.println (Iterator1.next ());} }
Execution Result:a........ab
Redis combat----Java using Redis