Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.logicbus.redis.toolkit;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import com.logicbus.redis.client.Connection;
import com.logicbus.redis.client.Toolkit;
import com.logicbus.redis.params.MigrateParams;
import com.logicbus.redis.params.ScanParams;
import com.logicbus.redis.result.ScanResult;
import com.logicbus.redis.util.BuilderFactory;
import com.logicbus.redis.util.RedisDataException;
import com.logicbus.redis.util.SafeEncoder;
/**
* Key相关的工具集
*
* @author duanyy
*
*/
public class KeyTool extends Toolkit {
public KeyTool(Connection _conn) {
super(_conn);
}
public static enum Command {
//常用
DEL,
EXISTS,
TYPE,
KEYS,
RENAME,
RENAMENX,
SCAN,
//生命期
EXPIRE,
EXPIREAT,
TTL,
PERSIST,
PEXPIRE,
PEXPIREAT,
PTTL,
//很少用
DUMP,
RESTORE,
MIGRATE,
MOVE,
OBJECT,
RANDOMKEY;
public final byte [] raw;
Command(){
raw = SafeEncoder.encode(name());
}
}
/**
* to delete keys
* @param keys
*/
public void _del(final String... keys){
sendCommand(Command.DEL.raw, keys);
}
/**
* to delete keys
* @param keys
* @return the number of keys that were removed.
*/
public long del(final String... keys){
_del(keys);
return getIntegerReply();
}
/**
* to determine if a key exists
* @param key
*/
public void _exist(final String key){
sendCommand(Command.EXISTS.raw,key);
}
/**
* to determine if a key exists
* @param key
* @return true if exists otherwise false
*/
public boolean exist(final String key){
_exist(key);
return getIntegerReply() > 0;
}
/**
* to determine the type stored at key
* @param key
*/
public void _type(final String key){
sendCommand(Command.TYPE.raw,key);
}
/**
* to determine the type stored at key
* @param key
* @return type of key, or none when key does not exist.
*/
public String type(final String key){
_type(key);
return getStatusCodeReply();
}
/**
* to get all the keys matching pattern
*
*
* The time complexity is O(N) with N being the number of keys in the database.
* It should be used carefully.
*
* @param pattern
*/
public void _keys(final String pattern){
sendCommand(Command.KEYS.raw,pattern);
}
/**
* to get all the keys matching pattern
*
*
* The time complexity is O(N) with N being the number of keys in the database.
* It should be used carefully.
*
* @param pattern
* @return list of keys matching pattern.
*/
public Set keys(final String pattern){
_keys(pattern);
return BuilderFactory.STRING_SET.build(
getBinaryMultiBulkReply(),null
);
}
/**
* to rename the key to a new key
* @param key
* @param newKey
*/
public void _rename(final String key,final String newKey){
sendCommand(Command.RENAME.raw,key,newKey);
}
/**
* to rename the key to a new key
* @param key
* @param newKey
*/
public boolean rename(final String key,final String newKey){
_rename(key,newKey);
try {
getStatusCodeReply();
return true;
}catch (RedisDataException ex){
return false;
}
}
/**
* to rename the key to a new key if the new key does not yet exist.
* @param key
* @param newKey
*/
public void _renamenx(final String key,final String newKey){
sendCommand(Command.RENAMENX.raw,key,newKey);
}
/**
* to rename the key to a new key if the new key does not yet exist.
* @param key
* @param newKey
* @return true if the key was renamed
*/
public boolean renamenx(final String key,final String newKey){
_renamenx(key,newKey);
return getIntegerReply() > 0;
}
/**
* to scan the keys
* @param cursor
* @param params
*/
public void _scan(final String cursor,ScanParams params){
final List args = new ArrayList();
args.add(SafeEncoder.encode(cursor));
args.addAll(params.getParams());
sendCommand(Command.SCAN.raw, args.toArray(new byte[args.size()][]));
}
/**
* to scan the keys
* @param cursor
* @param params
*/
@SuppressWarnings("unchecked")
public ScanResult scan(final String cursor,ScanParams params){
_scan(cursor,params);
List