Jedis operation Redis (rpm)

package org.jzkangta.jedis;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


import redis.Clients . jedis . Jedis ;

public class JedisDemo {

public void test1 () {
Jedis Redis = new new Jedis ( "192.168.10.64" , 6379 ); // connected Redis
Redis . the auth ( "Redis" ); // Verify Password
/ * -------------------------------------------------- -------------------------------------------------- * ------- /
/ ** KEY operation

// KEYS
the Set Keys = redis.keys ( "*"); // list all of the key, to find a specific key, such as: redis.keys ( "foo" )
the Iterator keys.iterator T1 = ();
the while (t1.hasNext ()) {
Object OBJ1 t1.next = ();
System.out.println (OBJ1);
}

// remove DEL given one or more of the key. If the key does not exist, ignore the command.
redis.del ( "NAME1");

// returns the TTL given key remaining survival time (time to live) (in seconds)
redis.ttl ( "foo");

remove // PERSIST key of a given key survival time.
redis.persist ( "foo");

// EXISTS check whether a given key exists.
redis.exists ( "foo");

// db the MOVE key moves the current database key (default 0) to a given database db them. If the current database (source database) and a given database (the target database) with the same name given key, or key does not exist in the current database, then MOVE has no effect.
redis.move ( "foo", 1) ; // foo with the key, moves into the database. 1

// the RENAME key to key the newkey renamed newkey. Newkey and when the same key or key does not exist, an error is returned. When newkey already exists, RENAME command overwrites the old value.
redis.rename ( "foo", "foonew ");


System.out.println (redis.type ( "foo") ); // none (key not present), string (string), list (list), set (collection), zset (ordered set), hash ( hash table)

// EXPIRE seconds The key for the given key settings survival time. When the key expires, it is automatically deleted.
redis.expire ( "foo", 5) ; // 5 Miao expired
// EXPIREAT EXPIREAT role and EXPIRE as are used to set the survival time is key. Except that the time parameter EXPIREAT accepted command is a UNIX timestamp (unix timestamp).

// SORT general usage the easiest method is to use SORT SORT key.
redis.lpush ( "Sort", ". 1");
redis.lpush ( "Sort", ". 4");
redis.lpush ( "Sort", ". 6");
redis.lpush ( "Sort", ". 3" );
redis.lpush ( "Sort", "0");

List List = redis.sort ( "Sort"); // default is ascending
for (int i = 0; i <list.size (); i ++) {
System.out.println (list.get (i));



/ * ------------------------------------------------ -------------------------------------------------- * --------- /
/ ** sTRING operation

// SET key value associated with the string value to the value key.
redis.set ( "name", "wangjun1");
redis.set ( "ID", "123456");
redis.set ( "address", "Guangzhou");

// Key SETEX seconds The value associated with the value of the value key, and the key to the survival time seconds (in seconds).
redis.setex ( "foo",. 5, "haha");

// Key value MSET [Key value ...] simultaneously providing one or more key-value pair.
redis.mset ( "haha", "111", "xixi", "222");

//redis.flushAll (); Clear All Key
System.out.println (redis.dbSize ());

// dbSize key number is the number of the APPEND if key after the key value is a string already exists, APPEND command value is added to the original value of the key //.
( "foo", "00" ) redis.append; // if the key is a string already exists, APPEND command value is added to an original value after key.

// GET key returns a string value associated key
redis.get ( "foo");

// the MGET key [key ...] Returns all (one or more) of the given key value
List list = redis.mget ( "haha", "xixi");
for (int I = 0; I <list.size (); I ++) {
System.out.println (List.get (I));
}

// DECR key in the key the stored digital value minus one.
// DECRBY key decrement the key stored value minus decrement decrement.
// INCR key digital value by a stored key.
// INCRBY key increment the stored key value plus the increment increment.

* /
/ * ---------------------------------------------- -------------------------------------------------- * ----------- /
/ ** the hash operation

// HSET key field value of the field value of the field is set to the hash table key value.
redis.hset ( "Website", "Google", "www.google.cn");
redis.hset ( "Website",
redis.hset ( "Website", "siNA", "www.sina.com");

// Key Field HMSET value [Field value ...] simultaneously a plurality of field - value (Domain - value) set to ha Greek key in the table.
Map the HashMap = new new map ();
map.put ( "CardID", "123456");
map.put ( "username", "jzkangta");
redis.hmset ( "the hash", Map);

// Key Field HGET returns the hash table key field in the given field value.
System.out.println (redis.hget ( "the hash", "username"));

// key HMGET Field [Field ...] Returns a hash table key, the one or more values for a given domain.
= Redis.hmget List List ( "Website", "Google", "baidu", "siNA");
for (int I = 0; I <list.size (); I ++) {
System.out.println (List. get (i));
}

// Returns the hash table key HGETALL key, all of the fields and values.
Map <String, String> redis.hgetAll Map = ( "the hash");
for (entry of Map.Entry: EnumMap.entrySet ()) {
Of System.out.print (entry.getKey () + ":" + entry.getValue () + "\ T");
}

// the HDEL key Field [Field ...] delete key in a hash table or designated domain.
// HLEN key return key in a hash table the number of fields.
// HEXISTS key field to view the hash table key, a given domain field exists.
// HINCRBY key field increment the value of the domain field of the hash table key plus the increment increment.
// HKEYS key returns the hash table in all domains key.
// HVALS key returns all values in the hash table of key.

* /
/ * ---------------------------------------------- -------------------------------------------------- * ----------- /
/ ** the lIST operation
// LPUSH key value [value ...] the value of the key value inserted into the list header.
redis.lpush ( "List", "ABC");
redis.lpush ( "List", "XZC");
redis.lpush ( "List", "ERF");
redis.lpush ( "

// LRANGE key start stop returns the key elements in the list specified in the section, the section specifies an offset start and stop. Subscript (index) to both start and stop parameters 0 as a substrate, i.e., 0 indicates the first element in the list to a second list indicating the element 1, and so on. You can also use negative subscripts to the last element of the list represented -1, -2 represent the penultimate element of the list, and so on.
= Redis.lrange List List ( "List", 0, -1);
for (int I = 0; I <list.size (); I ++) {
System.out.println (List.get (I));
}

// LLEN key returns the length of the list of key.
// LREM key count value according to the value of the count parameter, equal to the removal of the list parameter value element.
* /
/ * ---------------------------------------------- -------------------------------------------------- * ----------- /
/ ** the sET operation
// SADD key member [member ...] the key member elements to set them.
redis.sadd ( "testset", "S1");
redis.sadd ( "testset", "S2");
redis.sadd ( "testset", "S3");

redis.sadd ( "testset", "S5");

// member SREM Key elements in the collection member is removed.
redis.srem ( "testset", "S5");

// SMEMBERS key return a collection of all the key members.
= Redis.smembers SET SET ( "testset");
the Iterator set.iterator T1 = ();
the while (t1.hasNext ()) {
Object OBJ1 t1.next = ();
System.out.println (OBJ1);
}

/ / SISMEMBER key member to determine whether the member is a set of key elements of the members. It is (true), or (to false)
System.out.println (redis.sismember ( "testset", "S4"));

// set SCARD key return key cardinality (number of elements in the set).
// SMOVE source destination member to the mobile member from the source element to the destination set collection.

// SINTER key [key ...] returns all members of a collection, the collection is the intersection of all the given collection.
// SINTERSTORE destination key [key ...
// SUNION key [key ...] returns all members of a collection, the collection is all a given set of union.
// SUNIONSTORE destination key [key ...] This command is equivalent to SUNION, but it will save the results to a destination collection, rather than simply return a result set.
// SDIFF key [key ...] returns all members of a collection, the collection is the difference between a given set of all sets.
// SDIFFSTORE destination key [key ...] This command is equivalent to SDIFF, but it will save the results to a destination collection, rather than simply return a result set.

* /


}



/ **
* @param args
* /
public static void main ( String [] args ) {

JedisDemo T1 = new new JedisDemo ();
T1 . Test1 ();
}

}

Reproduced in: https: //www.cnblogs.com/xu-thinking/p/3399108.html

Guess you like

Origin blog.csdn.net/weixin_34259159/article/details/93374816