About Redis Usage

Source: Internet
Author: User

Use under 1.window

: Http://pan.baidu.com/s/1gdCoKyn

Then run Redis-server.exe

Start a good service as follows,

2. Open CMD window, CD to directory C:\Users\Administrator\Desktop\redis-2.4.5-win32-win64\32bit

Execution: Redis-cli.exe (client)

3.redis command-Data storage introduction

Http://redis.io/topics/data-types-intro

The example of the official website is best done once, can quickly learn redis, in addition to the last bitmap, lexicograpical these more difficult to understand, but also not often used to, the basic is relatively easy to understand

4.Java Access Redis-jedius

Jedis-2.7.2.jar:http://pan.baidu.com/s/1o6mxyim

Give a jedius-api:http://tool.oschina.net/uploads/apidocs/

Then give a Java wrapper class:

import java.util.arraylist;import java.util.list;import java.util.set;import  redis.clients.jedis.jedis;import redis.clients.jedis.jedispool;public class redistool { private static string redishost =  "127.0.0.1";p Rivate static jedispool  pool = new jedispool (redishost);// get command, the default selection of the database is 0public static object  get (String key)  {Jedis jedis = null;try {jedis =  Pool.getresource (); Byte[] data = jedis.get (Key.getbytes ());if  (data == null  | |  data.length <= 0)  {return null;} Return serializeutil.unserialize (data);}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);}}  get command, the second parameter is select Database Public static object get (string key, int dbindex)   {jedis jedis  = null;try {jedis = pool.getresource (); Jedis.select (dbindex); byte[] data =  jedis.get (Key.getbytes ());if  (data == null | |  data.length <= 0)  {return null;} Return serializeutil.unserialize (data);}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);}}  set command, the default selection is 0public static void set (string key, object value)  { Jedis jedis = null;try {jedis = pool.getresource (); Jedis.set (Key.getBytes (),  serializeutil.serialize (value));}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);}}  set command, the second parameter is to select a database Public static void set (string key, object value,  Int dbindex) &NBSP;{JEDIS&NBSP;JEDIS&NBSP;=&NBSP;NULL;TRY&NBSP;{JEDIS&NBSP;=&NBSp;pool.getresource (); Jedis.select (Dbindex); Jedis.set (Key.getbytes (),  serializeutil.serialize (value));}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);}} The  hset command is an ordinary Java-map object, key identifies a different map object, and field is the key value of the specified Map object Public static void hset (String  key, string field, object value)  {jedis jedis = null;try { Jedis = pool.getresource (); Jedis.hset (Key.getbytes (),  field.getbytes (),  Serializeutil.serialize (value));}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);}}  hget is the Field field value of the map object that gets the specified key value Public static object hget (string key, string  field)  {jedis jedis = null;try {jedis = pool.getresource (); byte[]  data = jedis.hget (Key.getbytes (),  field.geTbytes ());if  (data == null | |  data.length <= 0)  {return null;} Return serializeutil.unserialize (data);}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);}}  remove to remove a record Public static void remove (String key)  {jedis jedis =  null;try {jedis = pool.getresource (); Jedis.del (Key.getbytes ());}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);}}  redis implementation of automatic +1 sequence acquisition public static long getautoid (String key)  {jedis jedis  = null;long id = 1;try {jedis = pool.getresource ();id =  JEDIS.INCR (Key.getbytes ());}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);}Return id;}  list-push,key identifies a list object and is Left-pushpublic static void lpush (string key,  Object value)  {jedis jedis = null;try {jedis = pool.getresource (); Jedis.lpush (Key.getbytes (),  serializeutil.serialize (value));}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);}}  list-push,key identifies a list object and is Right-pushpublic static void rpush (string key,  Object value)  {jedis jedis = null;try {jedis = pool.getresource (); Jedis.rpush (Key.getbytes (),  serializeutil.serialize (value));}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);}}   Gets all elements of the List object of the specified key value Public static list<object> lrange (String key)  { jedis jedis = null; list<object&Gt; result = null;try {jedis = pool.getresource (); List<byte[]> list = jedis.lrange (Key.getbytes (),  0, -1); result = new  ArrayList<Object> (List.size ());for  (byte[] o : list)  {result.add ( Serializeutil.unserialize (o));}}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);} Return result;}  left-pop A list object that specifies a key value Public static object lpop (String key)  {Jedis  Jedis = null;object object = null;try {jedis = pool.getresource (); byte [] bs = jedis.lpop (Key.getbytes ()); Object = serializeutil.unserialize (BS);}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);} Return object;}  right-pop A list object that specifies a key value Public statiC object rpop (String key)  {jedis jedis = null;object object =  null;try {jedis = pool.getresource (); Byte[] bs = jedis.rpop (Key.getBytes () ); Object = serializeutil.unserialize (BS);}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);} Return object;}  list-index, finds the index element of the list object that specifies the key value Public static object lindex (string key,  Int index)  {jedis jedis = null;object object = null;try {jedis  = pool.getresource (); Byte[] bs = jedis.lindex (Key.getbytes (),  index), Object  = serializeutil.unserialize (BS);}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {close (Jedis);} Return object;}   Close Jedisprivate static void closE (Jedis jedis)  {if  (jedis != null)  {jedis.close ();}}  list-length, gets the number of elements of the list object that specifies the key value Public static long llen (String key)  {jedis  jedis = null;long object;try {jedis = pool.getresource (); object =  jedis.llen (Key.getbytes ());}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {if  (Jedis != null)  {jedis.close ();}} Return object;}   Get all keys for the current database (the default ordinal is 0), equivalent to the "keys *" Command Public static list<string> keys ()  { jedis jedis = null; List<string> list = null;try {jedis = pool.getresource (); Set<byte[]> set = jedis.keys ("*". GetBytes ());list = new arraylist< String> (Set.size ());for  (Byte[] bs : set)  {list.add (new string (bs, ) UTF8 "));}}  catch  (exception e)  {throw new runtimeexception ("Redis Error! ",  e);}  finally {if  (Jedis != null)  {jedis.close ();}} Return list;}}

5.linux installation of Redis

The installation package is redis-3.0.4.tar.gz, given: http://pan.baidu.com/s/1jGm0ncy

First CD to/usr/local/, then mkdir a folder Redis, then copy redis-3.0.4.tar.gz to /usr/local/redis

Unzip, perform TAR-CXVF redis-3.0.4.tar.gz

After CD to redis-3.0.4, execute make command

Then execute the Make install command

After completion the Redis service automatically starts, this time my Redis service is deployed on 210.10.5.189, native IP is 210.10.5.102

Open the CMD window, CD to the directory that has Redis-cli.exe, C:\Users\Administrator\Desktop\redis-2.4.5-win32-win64\32bit, and then execute Redis-cli.exe -H 210.10.5.189-p 6379, as follows

It's no different than deploying on Windows.

6. A few additional notes

6.1 Commands

flushdb--Delete all keyflushall--in the current database delete key from all databases

6.2 Linux-redis View Startup process:
Ps-ef | grep Redis

6.3 Configuring Redis Maximum allocated memory

Vim redis.conf

Modify MaxMemory <bytes> for maxmemory <2147483648>

To prevent all memory from being consumed by Redis, when the maximum memory setting is reached, Redis will first attempt to clear expired or expiring keys, and will remove some key-value from free-list. When none of the above methods are effective, Redis no longer supports write operations (such as Set,lpush), but does not affect read operations such as get. (maxmemory configuration is commented out by default, no limit, same as RAM)

About Redis Usage

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.