Usage of redis

Source: Internet
Author: User
First access redis. Io to download the latest stable version 2.2.12, then follow the steps below 0, tar-xvzf redis-2.2.12.tar.gz
1, CD redis-2.2.12
2, make
3, edit redis/redis. conf, set maxmemory 100 MB (here, we just for test, so this size is set to 100 MB)
4, sudo sysctl VM. overcommit_memory = 1
5. src/redis-server redis. conf (Start up server)
6. src/redis-cli (start up the client)
7, test on redis:
Mingyuan @ mingyuan-LAPTOP:/opt/redis-2.2.12 $ src/redis-cli
Redis 127.0.0.1: 6379> Set M 1
OK
Redis 127.0.0.1: 6379> Get m
"1"
Redis 127.0.0.1: 6379> exit
Mingyuan @ mingyuan-LAPTOP:/opt/redis-2.2.12 $ the second part uses Jedis to operate redis without talking nonsense, directly on Code : Package CN. mingyuan. redis;

Import java. Io. bytearrayinputstream;
Import java. Io. bytearrayoutputstream;
Import java. Io. ioexception;
Import java. Io. objectinputstream;
Import java. Io. objectoutputstream;

Import redis. Clients. Jedis. Jedis;

Public class test {

/**
* @ Param ARGs
* @ Throws ioexception
* @ Throws classnotfoundexception
*/
Public static void main (string [] ARGs) throws ioexception,
Classnotfoundexception {
Jedis redis = new Jedis ("localhost ");
// Connect is optional, because the set operation is performed first to determine whether the client has established a connection with the server. If not, the connection process is started.
Redis. Connect ();
String set = redis. Set ("mingyuan", "1 ");
System. Out. println ("set Result \ t" + set );
Redis. incr ("mingyuan ");
String string = redis. Get ("mingyuan ");
System. Out. println ("Get result of key 'mingyuanc' \ t" + String );

// The following is the test code for storing objects
bytearrayoutputstream Bos = new bytearrayoutputstream ();
objectoutputstream OOS = new objectoutputstream (BOS);
person = new person ();
person. setage (1);
person. setname ("mingyuan");
OOS. writeobject (person);
byte [] bytearray = Bos. tobytearray ();
OOS. close ();
Bos. close ();
string setobjectret = redis. set ("mingyuan ". getbytes (), bytearray);
system. out. println ("set object return \ t" + setobjectret);

Byte [] BS = redis. Get ("mingyuan". getbytes ());

Bytearrayinputstream Bis = new bytearrayinputstream (BS );
Objectinputstream inputstream = new objectinputstream (bis );
Person readobject = (person) inputstream. readobject ();
System. Out. println ("read object \ t" + readobject. tostring ());
Inputstream. Close ();
Bis. Close ();

Redis. Disconnect ();

}

} Person class: Package CN. mingyuan. redis;

Import java. Io. serializable;
/**
* Pojo is used for testing to implement serializable for serialization.
* @ Author mingyuan
*
*/
Public class person implements serializable {
Private Static final long serialversionuid =-3562550857760039655l;
@ Override
Public String tostring (){
Return "person [name =" + name + ", age =" + age + "]";
}

Private string name;
Private int age;
Public String getname (){
Return name;
}

Public void setname (string name ){
This. Name = Name;
}

Public int getage (){
Return age;
}

Public void setage (INT age ){
This. Age = age;
}
} Output result: Set resultOK
Get result of key 'mingyuan'2
Set object returnOK
Read objectPerson [name = mingyuan, age = 1] The third part summarizes redis, as a popular K/V database, which is easy to operate, easy to deploy, and has a good Java client (Jedis ), many things worth exploring will be shared in the future.

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.