RedisTemplate Common Methods

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/sinat_22797429/article/details/89196933

Many companies will redisTemplate the package, packaged into a convenient RedisUtil tools needed to conduct business calls, this article summarizes some commonly used methods redisTemplate.

Redis common data types:

  • String
  • Hash
  • List
  • Set
  • token
  • Sorted set

String type

Determining whether a key value corresponding with Returns true, false if there is no

redisTemplate.hasKey(key)

Has a value corresponding to the key value is extracted

redisTemplate.opsForValue().get(key)

Delete a single key value

redisTemplate.delete(key)

Batch delete key

redisTemplate.delete(keys) //其中keys:Collection<K> keys

The current value of the incoming sequence into a key type byte []

redisTemplate.dump(key)

Set an expiration time

public Boolean expire(String key, long timeout, TimeUnit unit) {
    return redisTemplate.expire(key, timeout, unit);
 }
 public Boolean expireAt(String key, Date date) {
    return redisTemplate.expireAt(key, date);
  }

Find a matching key values, return a collection type Set

public Set<String> getPatternKey(String pattern) {
    return redisTemplate.keys(pattern);
}

Redis change the name of the key

 public void renameKey(String oldKey, String newKey) {
    redisTemplate.rename(oldKey, newKey);
}

Key returns the passed value type of the stored

public DataType getKeyType(String key) {
    return redisTemplate.type(key);
}

If the old value exists, the old value to the new value

public Boolean renameOldKeyIfAbsent(String oldKey, String newKey) {
    return redisTemplate.renameIfAbsent(oldKey, newKey);
}

Taken randomly from a key in redis

redisTemplate.randomKey()

Key returns the remaining expiration time corresponding to the current

 public Long getExpire(String key) {
    return redisTemplate.getExpire(key);
}

Returns the remaining time and the expiration time specified unit

public Long getExpire(String key, TimeUnit unit) {
    return redisTemplate.getExpire(key, unit);
}

The key persistently saved

public Boolean persistKey(String key) {
    return redisTemplate.persist(key);
}

Key to move the current database to the specified database among redis

public Boolean moveToDbIndex(String key, int dbIndex) {
    return redisTemplate.move(key, dbIndex);
}

Set the current key and value value

redisTemplate.opsForValue().set(key, value)

Sets the current value and the key value, and set an expiration time

redisTemplate.opsForValue().set(key, value, timeout, unit)

Return to sub-character string of key

public String getCharacterRange(String key, long start, long end) {
    return redisTemplate.opsForValue().get(key, start, end);
}

The old key set value, and returns the old key

public String setKeyAsValue(String key, String value) {
    return redisTemplate.opsForValue().getAndSet(key, value);
}

Batch acquisition value

 public List<String> multiGet(Collection<String> keys) {
    return redisTemplate.opsForValue().multiGet(keys);
 }

On the basis of the original value of the new string to the end

redisTemplate.opsForValue().append(key, value)

Incrementally double value stored in the variable

 public Double incrByDouble(String key, double increment) {
    return redisTemplate.opsForValue().increment(key, increment);
 }

By increment (K key, long delta) method increments the value stored long (the increment value, the negative decrementing)

public Long incrBy(String key, long increment) {
    return redisTemplate.opsForValue().increment(key, increment);
}

If the corresponding map collection name does not exist, add or not modified

Map valueMap = new HashMap();  
valueMap.put("valueMap1","map1");  
valueMap.put("valueMap2","map2");  
valueMap.put("valueMap3","map3");  
redisTemplate.opsForValue().multiSetIfAbsent(valueMap); 

Setting map to set redis

Map valueMap = new HashMap();  
valueMap.put("valueMap1","map1");  
valueMap.put("valueMap2","map2");  
valueMap.put("valueMap3","map3");  
redisTemplate.opsForValue().multiSet(valueMap);  

Get the length of the string

redisTemplate.opsForValue().size(key)

Cover addressed by the value parameter values ​​given string stored key, starting at offset offset

redisTemplate.opsForValue().set(key, value, offset)

Reset key corresponding to the value, if it returns false, true otherwise

redisTemplate.opsForValue().setIfAbsent(key, value)

The value of the value associated with the key, and the key expiration time set timeout

redisTemplate.opsForValue().set(key, value, timeout, unit)

The binary value of the offset value becomes a bit

redisTemplate.opsForValue().setBit(key, offset, value)

Key string stored value, obtaining bits (bit) on the specified offset

redisTemplate.opsForValue().getBit(key, offset)

Hash type

Redis hash field is a string type and the value of the mapping table, hash is particularly suited for storing objects.
Redis hash may be stored in each of 2 ^ 32--1 key-value pair (40 billion).

Whether to acquire key variable has a value specified map, if the map button to get the value exists, no null is returned.

redisTemplate.opsForHash().get(key, field)

Gets variable key-value pairs

public Map<Object, Object> hGetAll(String key) {
    return redisTemplate.opsForHash().entries(key);
}

New hashMap value

redisTemplate.opsForHash().put(key, hashKey, value)

Add key-value pairs in the form of a collection of map

public void hPutAll(String key, Map<String, String> maps) {
    redisTemplate.opsForHash().putAll(key, maps);
}

Only when the setting is not present when hashKey

public Boolean hashPutIfAbsent(String key, String hashKey, String value) {
    return redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
}

Delete one or more of the hash table fields

public Long hashDelete(String key, Object... fields) {
    return redisTemplate.opsForHash().delete(key, fields);
}

View the hash table specified field is present

public boolean hashExists(String key, String field) {
    return redisTemplate.opsForHash().hasKey(key, field);
}

To the integer value of the specified field in the hash table key plus delta increment

public Long hashIncrBy(String key, Object field, long increment) {
    return redisTemplate.opsForHash().increment(key, field, increment);
}
 public Double hIncrByDouble(String key, Object field, double delta) {
    return redisTemplate.opsForHash().increment(key, field, delta);
}

Get all fields in the hash table

redisTemplate.opsForHash().keys(key)

Gets the number of fields in the hash table

redisTemplate.opsForHash().size(key)

Get all the values ​​that exist in the hash table

public List<Object> hValues(String key) {
    return redisTemplate.opsForHash().values(key);
}

Get matching key-value pairs, ScanOptions.NONE to acquire all of the key

public Cursor<Entry<Object, Object>> hashScan(String key, ScanOptions options) {
    return redisTemplate.opsForHash().scan(key, options);
}

List type

Gets the list of elements by index

redisTemplate.opsForList().index(key, index)

Get a list of elements within a specified range (start start position, 0 is the start position, end position of the end, all return -1)

redisTemplate.opsForList().range(key, start, end)

Stored in the list head, i.e. to add a put it foremost index

redisTemplate.opsForList().leftPush(key, value)

The plurality of values ​​stored in the List (value may be a plurality of values, it may be a Collection value)

redisTemplate.opsForList().leftPushAll(key, value)

List presence of added time

redisTemplate.opsForList().leftPushIfPresent(key, value)

If the present value is added at a pivot at the front pivot

redisTemplate.opsForList().leftPush(key, pivot, value)

FIFO order to add (value may be a plurality of values, or Collection var2)

redisTemplate.opsForList().rightPush(key, value)
redisTemplate.opsForList().rightPushAll(key, value)

Adding value to the right pivot element

redisTemplate.opsForList().rightPush(key, pivot, value)

Setting the value at the specified index element

redisTemplate.opsForList().set(key, index, value)

Remove and get the first element of the list (if the list is not a list of the elements will block until a timeout or wait until the pop-up element can be found)

redisTemplate.opsForList().leftPop(key)
redisTemplate.opsForList().leftPop(key, timeout, unit)

Remove and get the last element in the list

redisTemplate.opsForList().rightPop(key)
redisTemplate.opsForList().rightPop(key, timeout, unit)

Pops an element from a queue on the right and another left into most of the elements specified queue

redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey)
redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey, timeout, unit)

Remove the set value is equal to the value of the element (index = 0, delete all elements equal value; index> 0, the head starts deleting a value from the first value is equal to the element; index <0, the first start deleting from the tail value equal to the value of the element)

redisTemplate.opsForList().remove(key, index, value)

List list will be cut

redisTemplate.opsForList().trim(key, start, end)

List Gets the current length of the list of key

redisTemplate.opsForList().size(key)

Set Types

Adding elements

redisTemplate.opsForSet().add(key, values)

Removing an element (a single value, multiple values)

redisTemplate.opsForSet().remove(key, values)

Delete and returns a random element

redisTemplate.opsForSet().pop(key)

Gets the size of the collection

redisTemplate.opsForSet().size(key)

Determining whether the set value comprises

redisTemplate.opsForSet().isMember(key, value)

Obtaining intersection of two sets (key corresponding to the unordered set of unordered collection otherKey corresponding intersection of)

redisTemplate.opsForSet().intersect(key, otherKey)

Obtaining a plurality of intersection of the sets (Collection var2)

redisTemplate.opsForSet().intersect(key, otherKeys)

otherKey intersection set of key set stored destKey set (which may be a single value or otherKey set)

redisTemplate.opsForSet().intersectAndStore(key, otherKey, destKey)

storing a plurality of key intersection set of sets of unordered set to destKey

redisTemplate.opsForSet().intersectAndStore(key, otherKeys, destKey)

Get and set two or more sets of (otherKeys may be a single value or a collection)

redisTemplate.opsForSet().union(key, otherKeys)

otherKey set of key set and the set of stored destKey (otherKeys may be a single value or a collection)

redisTemplate.opsForSet().unionAndStore(key, otherKey, destKey)

Obtaining two or more sets of difference set (otherKeys may be a single value or a collection)

redisTemplate.opsForSet().difference(key, otherKeys)

Stored in the difference-set destKey (otherKeys may be a single value or set)

redisTemplate.opsForSet().differenceAndStore(key, otherKey, destKey)

Acquiring a random element in the set

redisTemplate.opsForSet().randomMember(key)

Get all elements in the collection

redisTemplate.opsForSet().members(key)

Gets a collection of random elements in the count

redisTemplate.opsForSet().randomMembers(key, count)

Get elements (de-emphasis) a plurality of key unordered set, count represents the number of

redisTemplate.opsForSet().distinctRandomMembers(key, count)

Traversal set similar Interator (ScanOptions.NONE to show all)

redisTemplate.opsForSet().scan(key, options)

zSet type

ZSetOperations provides a series of methods to operate an ordered set
additive element (ordered set in accordance with the score values of the elements arranged in ascending)

redisTemplate.opsForZSet().add(key, value, score)

Delete the corresponding value, value may be a plurality of values

redisTemplate.opsForZSet().remove(key, values)

Value added score value of the element, and increase the return

redisTemplate.opsForZSet().incrementScore(key, value, delta)

Returns a set of ranked elements, arranged in ascending order is set score values ​​of the elements

redisTemplate.opsForZSet().rank(key, value)

Returns a collection of elements in the ranking, according to the score value was in the order of elements

redisTemplate.opsForZSet().reverseRank(key, value)

Gets a collection of elements in a given interval (start start position, end the end position, -1 to query all)

redisTemplate.opsForZSet().reverseRangeWithScores(key, start,end)

In accordance with the values ​​of elements in a set of queries Score, result from small to large

redisTemplate.opsForZSet().reverseRangeByScore(key, min, max)
redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max)
//返回值为:Set<ZSetOperations.TypedTuple<V>>

Descending sort acquired focus score elements between minimum and maximum values ​​of

redisTemplate.opsForZSet().reverseRangeByScore(key, min, max, start, end)

Get the number of elements based on the score value set

redisTemplate.opsForZSet().count(key, min, max)

Gets the size of the collection

redisTemplate.opsForZSet().size(key)
redisTemplate.opsForZSet().zCard(key)

Obtaining the key, value score value corresponding to the set of elements

redisTemplate.opsForZSet().score(key, value)

Members removed at the specified index

redisTemplate.opsForZSet().removeRange(key, start, end)

Removes the specified score range ensemble members

redisTemplate.opsForZSet().removeRangeByScore(key, min, max)

Get and set key and otherKey and stored in destKey (where otherKeys may be a single string or set of strings)

redisTemplate.opsForZSet().unionAndStore(key, otherKey, destKey)

And obtaining the intersection otherKey key and stored in the destKey (otherKeys which may be a single string or set of strings)

redisTemplate.opsForZSet().intersectAndStore(key, otherKey, destKey)

Through the collection (and iterator exactly the same)

Cursor<TypedTuple<Object>> scan = opsForZSet.scan("test3", ScanOptions.NONE);
        while (scan.hasNext()){
            ZSetOperations.TypedTuple<Object> item = scan.next();
            System.out.println(item.getValue() + ":" + item.getScore());
        }

Guess you like

Origin blog.csdn.net/sinat_22797429/article/details/89196933