Simple use of Redis-based memory databases

Source: Internet
Author: User

Customers using in-memory data in ecplise , if ready to download two jar Packages

Commons-pool2-2.0.jar

Jedis-2.4.2.jar

The premise is ready, so let's start the redis service , open a command form input such as the following command:redis-server or Redis-server Redis Root Mesh \redis.conf

Server is turned on, note that the end number is 6377

2. Create a project in Eclipse. Import the redist required package into the project

3. write a Jedis Tool class

Public class Jedisutil {

Private  Static            String HOST="127.0.0.1"; //Native address

Private  Static                   Integer PORT= 6379; //Service Port

Private  Static              Jedispoolconfig config; //Connection pool Configuration object

Private  Static                     Jedispool Pool; //Connection pool object

Static {

config = new jedispoolconfig ();

config. Setmaxidle (1024*10); //Set memory size

Pool = new jedispool (config,HOST);

}

/**

*

 *  @return get a Jedis object from the connection pool

*/

Public  Static Jedis Getpooljedis () {

return pool. getresource ();

}

/**

* Jedis objects are manually played back into the connection pool

*/

Public Static void Returnpooljedis (Jedis Jedis) {

pool. Returnresource (Jedis);

}

/**

 *  @return Create a Jedis connection directly

*/

Public Static Jedis Getjedis () {

return New Jedis (HOST, PORT);

}

}

4. Write a client class operation Jedis

Public class Client {

Public Static void Main (string[] args) {

Simpleset ();

MSet ();

}

Private Static void MSet () {

Note that the hypothesis uses Jedisutil.getjedis (); is to create a Jredis object directly, so it is not managed by the connection pool. So it can't be played back into the connection pool.

Jedis Jedis = Jedisutil.getpooljedis ();

Setting values for multiple values

Jedis.mset ("UserName","user1","pwd","123");

Take value

list<string> list = Jedis.mget ("UserName","pwd");

for (String string:list) {

System. out. println (string);

}

Jedis.flushdb ();

Jedisutil.returnpooljedis (Jedis);

}

Private Static void Simpleset () {

Jedis Jedis = Jedisutil.getpooljedis ();

Setting the value

Jedis.set ("UserName", "user1");

Take value

System. out. println ("userName:"+jedis.get ("userName"));

Emptying the in-memory database

Jedis.flushdb ();

Jedisutil.returnpooljedis (Jedis);

}

Simple operations for a post-complement collection

/** * Hash operation */private static void HashSet () {Jedis Jedis = Jedisutil.getpooljedis ();//set, sets the value of a field in the specified hash table Jedis.hset (" Key1 "," user1 "," 123 "), Jedis.hset (" Key2 "," User2 "," 456 "), Jedis.hset (" Key3 "," User3 "," 789 "),//get, Takes out the value of a field in the specified hash table System.out.println (Jedis.hget ("Key1", "user1")); /** * Hash map operation */public static void Mapset () {Jedis Jedis = Jedisutil.getpooljedis (); Map<string,string> map=new hashmap<string,string> () map.put ("user1", "123"), Map.put ("User2", "456"); Map.put ("User3", "789");//sets the key/value of a map collection to the hash table.    Jedis.hmset ("map", map); The value of the hash table field is taken out, as well as the map's Key//system.out.println (Jedis.hmget ("Map", "User3"));//Take out the hash table for multiple fields list<string> list = Jedis.hmget ("Map", "user1", "User2", "User3"); for (String string:list) {System.out.println ("value:" +string);}} /** * Returns the hash table key, all fields with the value */public static void GetAll () {Jedis Jedis = Jedisutil.getpooljedis (); map<string,string> map = jedis.hgetall ("map"); set<string> KeySet = Map.keyset ();iterator<string> i = Keyset.iterator (); whilE (I.hasnext ()) {System.out.println (Map.get (I.next ()));} Jedis.del (key) key field [field ...] Deletes one or more specified domains in the hash table key. Jedis.hlen (key) key returns the number of fields in the hash table key. Jedis.hexists (key, field) key field to view the hash table key, whether the given domain field exists.

Jedis.hincrbyfloat (Key, field, increment) key field increment the value of the field field in the hash table key plus the increment increment. Jedis.hkeys (key) key returns all fields in the hash table key. Jedis.hvals (key) key returns all the values in the hash table key. }/** * List operation */public static void list () {Jedis Jedis = Jedisutil.getpooljedis ();//Set value is the way the stack is stored, advanced and out.

Jedis.lpush (key, strings) strings can be set to multiple values. Jedis.lpush ("list", "Index1"), Jedis.lpush ("list", "Index2"), Jedis.lpush ("list", "index3");//value, Value list<string> list = Jedis.lrange ("list", 0,2) based on the range of subscript and end subscript, for (int i = 0; i < list.size (); i++) {SYSTEM.OUT.P Rintln (List.get (i)); Jedis.llen (key) key returns the length of the list key//jedis.lrem (key, count, value) to remove the value equal to the number of parameters in the list}/** * Set Operation */public static void Set () {Jedis Jedis = Jedisutil.getpooljedis ();//Set Value Jedis.sadd ("Set", "S1", "S2", "S3", "S4");//Remove an element to remove a , you can also move out multiple. Jedis.srem ("Set", "S1", "S2");//Get all the elements set<string> set = Jedis.smembers ("set");iterator<string> i = Set.iterator (); while (I.hasnext ()) System.out.println (I.next ());}




}

Data can now be valued in the redistclient

Open a command form, enter the command redis-cli Open the client, the original after the assignment

Simple use of Redis-based memory databases

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.