Java Operations Redis Simple example
Beginner Redis, in the Java language and environment to complete the Redis learning. First, the official website downloads the source code, compiles, installs, modifies the configuration file redis.conf three items: 1. Comment out bind 127.0.0.1 2. Daemonize no change to daemonize Yes 3. Protected-mode Yes to Protected-mode no so run redis-server redis.conf default is run in the background, and allow remote host to connect.
Using Jedis as a driver to complete the Java program Access to the Redis server, the test code is as follows:
Jedis Jedis = new Jedis ("192.168.100.103", 6379);
res = jedis.ping ();
System.out.println (RES);
The output pong indicates a successful connection.
1. Complete producer-consumer model using Redis's list data structure
Redis Server IP Port private static String Redisserverip = "192.168.100.103";
private static int redisserverport = 6379;
Producer data store queues private static String key_src = "Task-queue";
private static String key_tmp = "Tmp-queue";
public static class Producer implements runnable{Jedis Jedis = new Jedis (Redisserverip, Redisserverport);
public void Run () {while (true) {//Use UUID impersonation to produce a task String str = Uuid.randomuuid (). toString ();
Join Queue Jedis.lpush (KEY_SRC, str);
System.out.println ("Insert a new task:" + str + "Current task total:" + Jedis.llen (KEY_SRC));
try {Random Random = new Random ();
Thread.Sleep (Random.nextint (500) + 500);
catch (Interruptedexception e) {e.printstacktrace (); "}}} public static class Consumer implements Runnable {Jedis Jedis = new Jedis (Redisserverip, Redisserverp
ORT); public void Run () {while (true) {//Pull out the task String TaskID = Jedis.rpoplpush (KEY_SRC, key_tmp) from the queue;
if (TaskID!= null) {//Processing Task//.....
SYSTEM.OUT.PRINTLN ("Handle A new task:" + TaskID + "Current task total:" + Jedis.llen (KEY_SRC));
try {thread.sleep (1000);
catch (Interruptedexception e) {e.printstacktrace (); }}} public static void Main (string[] args) {new Thread (new Producer ()). Start (); Producer thread new Thread (new Consumer ()). Start ();
Consumer thread try {thread.sleep (long.max_value);
catch (Interruptedexception e) {e.printstacktrace (); }
}
2. Using the hash structure to complete the simplest shopping cart model
private static String Redisserverip = "192.168.100.103";
private static int redisserverport = 6379;
private static String Hash_name = "ShoppingCart";
public static void Main (string[] args) {Jedis Jedis = new Jedis (Redisserverip, Redisserverport);
Simulate the data map<string in the shopping cart, string> goodsmap = new hashmap<string, string> ();
Goodsmap.put ("Java", "5");
Goodsmap.put ("C + +", "3");
Goodsmap.put ("Node.js", "10");
Goodsmap.put ("C #", "10");
Hmset command Jedis.hmset (Hash_name, Goodsmap);
System.out.println ("Current Total" + Jedis.hlen (hash_name) + "a Fields");
list<string> fields = new arraylist<string> ();
Set<string> keyset = Jedis.hkeys (hash_name);
Iterator i = Keyset.iterator ();
while (I.hasnext ()) {Fields.Add (I.next ()). ToString ());
//hmget command list<string> List = Jedis.hmget (hash_name, Fields.get (0), Fields.get (1), Fields.get (2));
SYSTEM.OUT.PRINTLN (list); Hgetall Command map<string, string> map = Jedis.hgetall (hash_name);
set<entry<string, string>> entryset = Map.entryset ();
For (entry<string, string> entry:entryset) {System.out.println (Entry.getkey () + ":" + entry.getvalue ());
} jedis.disconnect (); }
3. Things to test
private static String Redisserverip = "192.168.100.103";
private static int redisserverport = 6379;
public static void Main (string[] args) {Jedis Jedis = new Jedis (Redisserverip, Redisserverport);
Long start = System.currenttimemillis ();
Open things Transaction Transaction = Jedis.multi ();
map<string, string> map = new hashmap<string, string> ();
Map.put ("username", "abcdefghijklmnopqrstuvwxyz");
Map.put ("Password", "123456");
Map.put ("Age", "25");
Map.put ("Gender", "man");
Map.put ("email", "XXXX@xxx.com");
Map.put ("Address", "Shenzhen, Guangdong Province, China");
Map.put ("tel", "18888888888");
Map.put ("position", "software Engineer");
Map.put ("Birth_date", "2016-01-01 00:00:00");
Map.put ("tel", "2016-01-01 00:00:00");
int totalrecords = 1024;
for (int i = 0; i < totalrecords i++) {String key = "User_" + i;
response<string> result = Transaction.hmset (key, map);
} Submitting things list<object> List = Transaction.exec ();
System.out.println ("Insert data Volume:" + list.size ());
Long end = System.currenttimemillis ();
System.out.println ("In total:" + ((End-start)/1000.0) + "seconds");
Jedis.disconnect (); }
4. Use Zset to complete the hottest commodity sorting function
private static String Redisserverip = "192.168.100.103";
private static int redisserverport = 6379;
private static String Producttopnkey = "PRODUCTTOPN";
Product Hot Search public static class Producthotn implements Runnable {Jedis Jedis = new Jedis (Redisserverip, Redisserverport);
Structured hot-search commodity string[] producttopn = {"IPhone7 plus", "P9 plus", "Xiaomi note", "Vivo X7 Plus", "Galaxy Note7"};
The change of search times of the simulator public void run () {while (true) {Random Random = new Random ();
Randomly pick a commodity String Product = Producttopn[random.nextint (5)];
The search was increased by 1 Jedis.zincrby (Producttopnkey, 1, product);
try {thread.sleep (500);
catch (Interruptedexception e) {e.printstacktrace (); Check the product Hot List public static class Hotviewer implements Runnable {Jedis Jedis = new Jedis (Redisserverip,
Redisserverport);
int i = 1;
public void Run () {while (true) {try {thread.sleep (1000); catch (Interruptedexception e){E.printstacktrace ();
} System.out.println ("The first" + i + "The second Fetch list");
set<tuple> set = Jedis.zrevrangewithscores (Producttopnkey, 0,-1);
for (Tuple tuple:set) {System.out.println (tuple.getelement () + ":" + Tuple.getscore ());
} i + +;
}} public static void Main (string[] args) {new Thread (new Producthotn ()). Start ();
New Thread (New Hotviewer ()). Start ();
try {thread.sleep (long.max_value);
catch (Interruptedexception e) {e.printstacktrace (); }
}