Redis与Tomcat集群集成

在网站访问量急剧上升时,通常需要使用集群的方法进行横向扩展。
对于没有状态的应用来说,直接用nginx进行处理即可。
但对于有状态的应用来说,比如登录状态等,除了使用nginx进行扩展外,就需要考虑到Session共享的问题了。

大家知道可以用apache+tomcat来实现Session共享,但效率太低了,而且容易出错。
今天说的主要是用nginx+tomcat+redis+tomcat-redis-session-manager的方式实现共享。
原理比较简单:

1、tomcat-redis-session-manage扩展了
org.apache.catalina.valves.ValveBase;
org.apache.catalina.session.ManagerBase;
org.apache.catalina.session.StandardSession;
并通过Tomcat配置,替代了这几个类。

2、Set属性时,用session id作为key,将Tomcat的整个Session拆分为SessionSerializationMetadata+RedisSession然后序列化为byte[],存放到Redis。

3、Get属性时,用session id作为key,从Redis获取byte[],然后反序列化为SessionSerializationMetadata+RedisSession,供Tomcat使用。

配置也很简单:
1、从github下载源码tomcat-redis-session-manager

2、用gradle进行编译

#master分支下面,要把signing段和uploadArchives段删掉,才能正常编译
#release就不需要了
gradle build

3、将三个Jar包拷贝到Tomcat的lib文件夹下

#%TOMCAT_HOME%/lib
tomcat-redis-session-manager-master-2.0.0.jar
commons-pool2-2.2.jar
jedis-2.5.2.jar

4、修改context.xml配置文件,新增下面内容就搞定咯

<!--%TOMCAT_HOME%/conf/context.xml-->
  <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
  <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" 
  host="localhost" port="6379" database="0" maxInactiveInterval="60"/>

好处是:不需要修改应用
坏处是:要耗费一定的时间来(序列化+保存到Redis)、(反序列化+从Redis读取)。

Redis分片(Jedis)

Redis的分片技术一般是通过客户端或代理来实现的

1、用jedis实现分片的时候,服务端不需要做任何配置即可

package com.djhu.redis.test;

import java.util.ArrayList;
import java.util.List;

import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;

public class JedisShardTest
{
	public static void main(String[] args)
	{
		List<JedisShardInfo> jedisShardInfoList = new ArrayList<JedisShardInfo>();
		jedisShardInfoList.add(new JedisShardInfo("172.16.172.4", 6379));
		jedisShardInfoList.add(new JedisShardInfo("172.16.172.4", 6380));

		ShardedJedis sharded = new ShardedJedis(jedisShardInfoList);
		sharded.set("key01", "a");
		sharded.set("key02", "b");
		sharded.set("key03", "c");
		sharded.set("key04", "d");
		sharded.set("key05", "e");
		
		System.out.println(sharded.get("key03"));
	}
}

2、用Jedis连接池实现分片

package com.djhu.redis.test;

import java.util.ArrayList;
import java.util.List;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.util.Hashing;
import redis.clients.util.Sharded;

public class JedisSharedFactory
{
	// 最大可用连接数,默认值为8,如果赋值为-1则表示不限制
	private static int MAX_TOTAL = 256;
	// 最大空闲连接数,默认值为8
	private static int MAX_IDLE = 32;
	// 最小空闲连接数
	private static int MIN_IDLE = 4;
	// 最大等待连接毫秒数,默认值为-1表示永不超时
	private static int MAX_WAIT = 3000;
	// 连接redis超时时间
	private static int TIMEOUT = 3000;
	// true表示验证连接
	private static boolean TEST_ON_BORROW = true;

	//连接池
	private static ShardedJedisPool jedisPool = null;
	public static void initJedisPool()
	{
		try
		{
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxTotal(MAX_TOTAL);
			config.setMaxIdle(MAX_IDLE);
			config.setMinIdle(MIN_IDLE);
			config.setMaxWaitMillis(MAX_WAIT);
			config.setTestOnBorrow(TEST_ON_BORROW);
			
			List<JedisShardInfo> jedisShardInfoList = new ArrayList<JedisShardInfo>();
			jedisShardInfoList.add(new JedisShardInfo("172.16.172.4", 6379));
			jedisShardInfoList.add(new JedisShardInfo("172.16.172.4", 6380));
			jedisPool = new ShardedJedisPool(config, jedisShardInfoList,Hashing.MURMUR_HASH,Sharded.DEFAULT_KEY_TAG_PATTERN);
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public synchronized static ShardedJedis getConnection()
	{
		try
		{
			if (jedisPool != null)
			{
				ShardedJedis resource = jedisPool.getResource();
				return resource;
			} else
			{
				return null;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static void returnResource(final ShardedJedis jedis)
	{
		if (jedis != null)
		{
			jedis.close();
		}
	}
	
	public static void main(String[] args)
	{
		initJedisPool();
		ShardedJedis redis = getConnection();
		redis.set("key10", "j");
		redis.set("key11", "k");
		redis.set("key12", "l");
		redis.set("key13", "m");
		redis.set("key14", "n");
		
		System.out.print(redis.get("key12"));
		
		returnResource(redis);
	}
}

Jedis连接Redis3 Cluster

1、源码如下

package com.djhu.redis.test;

import java.util.Set;
import java.util.HashSet;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class JedisClusterTest
{
	public static void main(String[] args)
	{
		Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();  
        jedisClusterNodes.add(new HostAndPort("172.16.172.4", 6379));  
        jedisClusterNodes.add(new HostAndPort("172.16.172.4", 6380));  
        jedisClusterNodes.add(new HostAndPort("172.16.172.4", 6381));  
        jedisClusterNodes.add(new HostAndPort("172.16.172.4", 6382));  
        jedisClusterNodes.add(new HostAndPort("172.16.172.4", 6383));  
        jedisClusterNodes.add(new HostAndPort("172.16.172.4", 7384));  
        
		//JedisCluster cluster = new JedisCluster(jedisClusterNodes,3000,1000);
        JedisCluster cluster = new JedisCluster(jedisClusterNodes);
		cluster.set("key10", "j");
		cluster.set("key11", "k");
		cluster.set("key12", "l");
		cluster.set("key13", "m");
		cluster.set("key14", "n");
		
		System.out.println(cluster.get("key12"));
		
	}
}

2、如果遇到下面错误,主要是因为建立cluster时,ip用了127.0.0.1。用其他ip重建一下cluster,就可以解决了。

Exception in thread "main" redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Cluster redirections?
	at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:34)
	at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:68)
	at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:85)
	at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:68)
	at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:85)
	at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:68)
	at redis.clients.jedis.JedisClusterCommand.run(JedisClusterCommand.java:29)
	at redis.clients.jedis.JedisCluster.set(JedisCluster.java:75)

Redis3配置Cluster

1、redis3的cluster是基于ruby的,所以要安装好ruby,然后安装redis的gem

gem install redis

2、然后配置6份redis,修改配置如下

节点
dbm6379 dbm6380 dbm6381 dbs6382 dbm6383 dbm6384
配置文件 dbm6379/conf/redis.conf dbm6380/conf/redis.conf dbm6381/conf/redis.conf dbs6382/conf/redis.conf dbm6383/conf/redis.conf dbm6384/conf/redis.conf
port 6379 6380 6381 6382 6383 6384
logfile “/home/neohope/DB/redis-3.0.4/cluster/dbm6379/logs/redis_log.log” “/home/neohope/DB/redis-3.0.4/cluster/dbm6380/logs/redis_log.log” “/home/neohope/DB/redis-3.0.4/cluster/dbm6381/logs/redis_log.log” “/home/neohope/DB/redis-3.0.4/cluster/dbm6382/logs/redis_log.log” “/home/neohope/DB/redis-3.0.4/cluster/dbm6383/logs/redis_log.log” “/home/neohope/DB/redis-3.0.4/cluster/dbm6384/logs/redis_log.log”
dir “/home/neohope/DB/redis-3.0.4/cluster/dbm6379/data” “/home/neohope/DB/redis-3.0.4/cluster/dbm6380/data” “/home/neohope/DB/redis-3.0.4/cluster/dbm6381/data” “/home/neohope/DB/redis-3.0.4/cluster/dbm6382/data” “/home/neohope/DB/redis-3.0.4/cluster/dbm6383/data” “/home/neohope/DB/redis-3.0.4/cluster/dbm6384/data”
cluster-enabled yes yes yes yes yes yes
cluster-config-file nodes-6379.conf nodes-6380.conf nodes-6381.conf nodes-6382.conf nodes-6383.conf nodes-6384.conf
cluster-node-timeout 15000 15000 15000 15000 15000 15000
cluster-migration-barrier 1 1 1 1 1 1
cluster-require-full-coverage yes yes yes yes yes yes

3、启动redis

#!/bin/sh
~/DB/redis-3.0.4/bin/redis-server ~/DB/redis-3.0.4/cluster/dbm6379/conf/redis.conf & echo $! & ~/DB/redis-3.0.4/bin/redis-server ~/DB/redis-3.0.4/cluster/dbm6380/conf/redis.conf & echo $! & ~/DB/redis-3.0.4/bin/redis-server ~/DB/redis-3.0.4/cluster/dbm6381/conf/redis.conf & echo $! & ~/DB/redis-3.0.4/bin/redis-server ~/DB/redis-3.0.4/cluster/dbs6382/conf/redis.conf & echo $! & ~/DB/redis-3.0.4/bin/redis-server ~/DB/redis-3.0.4/cluster/dbs6383/conf/redis.conf & echo $! & ~/DB/redis-3.0.4/bin/redis-server ~/DB/redis-3.0.4/cluster/dbs6384/conf/redis.conf & echo $!

4、配置cluster

#!/bin/sh
#这里最好不要用127.0.0.1做地址
~/DB/redis-3.0.4/bin/redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384      

5、测试redis cluster

~/DB/redis-3.0.4/bin/redis-cli -c -p 6379 
set key01 a
set key02 b
set key03 c
set key04 d
dbsize
keys *
get key03

6、关闭redis

#!/bin/sh
~/DB/redis-3.0.4/bin/redis-cli -p 6379 shutdown
~/DB/redis-3.0.4/bin/redis-cli -p 6380 shutdown
~/DB/redis-3.0.4/bin/redis-cli -p 6381 shutdown
~/DB/redis-3.0.4/bin/redis-cli -p 6382 shutdown
~/DB/redis-3.0.4/bin/redis-cli -p 6383 shutdown
~/DB/redis-3.0.4/bin/redis-cli -p 6384 shutdown

参考:
redis cluster tutorial

Redis主从数据库(Shell)

1、基本配置如下

数据库 master slave01 slave02
配置文件 redis.master.conf redis.slave01.conf redis.slave02.conf
ip地址 localhost localhost localhost
端口 6379 6380 6381
logfile “D:/Database/Redis2.8/mirror/master/logs/redis_log.txt” “D:/Database/Redis2.8/mirror/slave01/logs/redis_log.txt” “D:/Database/Redis2.8/mirror/slave02/logs/redis_log.txt”
dir “D:/Database/Redis2.8/mirror/master/data/” “D:/Database/Redis2.8/mirror/slave01/data/” “D:/Database/Redis2.8/mirror/slave02/data/”
slaveof localhost 6379 localhost 6379
slave-serve-stale-data yes yes
slave-read-only yes yes
slave-priority 100 100
maxheap 1073741824 1073741824
heapdir D:\Database\Redis2.8\mirror\slave01\heap D:\Database\Redis2.8\mirror\slave02\heap

2、启动

redis-server.exe D:\Database\Redis2.8\mirror\redis.master.conf
redis-server.exe D:\Database\Redis2.8\mirror\redis.slave01.conf
redis-server.exe D:\Database\Redis2.8\mirror\redis.slave02.conf

3、测试

D:\Database\Redis2.8>redis-cli
127.0.0.1:6379> dbsize
(integer) 0
127.0.0.1:6379> set key01 a
OK
127.0.0.1:6379> exit

D:\Database\Redis2.8>redis-cli -p 6380
127.0.0.1:6380> get key01
"a"
127.0.0.1:6380> set key02 b
(error) READONLY You can't write against a read only slave.
127.0.0.1:6380> exit

D:\Database\Redis2.8>redis-cli -p 6381
127.0.0.1:6381> get key01
"a"
127.0.0.1:6381> set key02 b
(error) READONLY You can't write against a read only slave.
127.0.0.1:6381> exit

4、关闭

redis-cli -p 6380 shutdown
redis-cli -p 6381 shutdown
redis-cli -p 6379 shutdown

Jedis使用连接池(Java)

package com.djhu.redis.test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisFactory
{
	// 最大可用连接数,默认值为8,如果赋值为-1则表示不限制
	private static int MAX_TOTAL = 256;
	// 最大空闲连接数,默认值为8
	private static int MAX_IDLE = 32;
	// 最小空闲连接数
	private static int MIN_IDLE = 4;
	// 最大等待连接毫秒数,默认值为-1表示永不超时
	private static int MAX_WAIT = 3000;
	// 连接redis超时时间
	private static int TIMEOUT = 3000;
	// true表示验证连接
	private static boolean TEST_ON_BORROW = true;

	//连接池
	private static JedisPool jedisPool = null;
	public static void initJedisPool(String IP, int port, String password)
	{
		try
		{
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxTotal(MAX_TOTAL);
			config.setMaxIdle(MAX_IDLE);
			config.setMinIdle(MIN_IDLE);
			config.setMaxWaitMillis(MAX_WAIT);
			config.setTestOnBorrow(TEST_ON_BORROW);
			
			jedisPool = new JedisPool(config, IP, port, TIMEOUT, password);
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public synchronized static Jedis getConnection()
	{
		try
		{
			if (jedisPool != null)
			{
				Jedis resource = jedisPool.getResource();
				return resource;
			} else
			{
				return null;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static void returnResource(final Jedis jedis)
	{
		if (jedis != null)
		{
			jedis.close();
		}
	}
	
	public static void main(String[] args)
	{
		initJedisPool("localhost",6379,null);
		Jedis redis = getConnection();
		redis.select(1);
		//redis.set("key01", "a");
		//redis.set("key02", "b");
		//redis.set("key03", "c");
		System.out.print(redis.dbSize());
		
		returnResource(redis);
	}
}

Redis入门之增删改查(Java)

1、下载驱动
jedis驱动源码地址
jedis驱动下载地址

2、测试代码

package com.djhu.redis.test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import redis.clients.jedis.Jedis;

public class ConnectionTest
{
	public static Jedis getConnection(String ip, int port)
	{
		Jedis jedis = new Jedis(ip, port);
		//jedis.auth("password");
		return jedis;
	}

	public static void cleanAll(Jedis jedis)
	{
		jedis.flushDB();
	}

	public static void stringTest(Jedis jedis)
	{
		jedis.set("key01", "a");
		jedis.set("key02", "b");
		jedis.set("key03", "c");
		jedis.mset("key04", "d", "key05", "e", "key06", "f");

		jedis.del("key04");
		System.out.println("key01 is " + jedis.get("key01"));
		System.out.println("key04 is " + jedis.get("key04"));
	}

	public static void mapTest(Jedis jedis)
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", "hansen");
		map.put("usersex", "male");
		jedis.hmset("mapkey01", map);

		map.put("username", "neohope");
		map.put("usersex", "male");
		jedis.hmset("mapkey02", map);

		map.put("username", "tuzi");
		map.put("usersex", "female");
		jedis.hmset("mapkey03", map);

		List<String> rsmap = jedis.hmget("mapkey03", "username", "usersex");
		System.out.println("query for hmget(\"mapkey03\", \"username\", \"usersex\") is " + rsmap);

		jedis.hdel("mapkey02", "usersex");
		System.out.println("query for hmget jedis.hmget(\"mapkey02\", \"username\") is "+jedis.hmget("mapkey02", "username"));
		System.out.println("query for jedis.hmget(\"mapkey02\", \"usersex\") is " + jedis.hmget("mapkey02", "usersex"));

		System.out.println("query for jedis.hlen(\"mapkey01\") is " + jedis.hlen("mapkey01"));
		System.out.println("query for jedis.exists(\"mapkey01\") is " + jedis.exists("mapkey01"));
		System.out.println("query for jedis.hkeys(\"mapkey01\") is " + jedis.hkeys("mapkey01"));
		System.out.println("query for jedis.hvals(\"mapkey01\") is " + jedis.hvals("mapkey01"));
	}

	public static void listTest(Jedis jedis)
	{
		jedis.lpush("keylist01", "a");
		jedis.lpush("keylist01", "b");
		jedis.lpush("keylist01", "c");

		System.out.println("keylist01 is " + jedis.lrange("keylist01", 0, -1));
	}

	public static void setTest(Jedis jedis)
	{
		jedis.sadd("keyset01", "01");
		jedis.sadd("keyset01", "02");
		jedis.sadd("keyset01", "03");
		jedis.sadd("keyset01", "04");
		jedis.sadd("keyset01", "05");

		System.out.println("query for jedis.smembers(\"keyset01\") is " + jedis.smembers("keyset01"));
		System.out.println("query for jedis.sismember(\"keyset01\", \"06\") is " + jedis.sismember("keyset01", "06"));
		System.out.println("query for jedis.scard(\"keyset01\") is " + jedis.scard("keyset01"));
	}

	public static void sortTest(Jedis jedis)
	{
		jedis.rpush("keylist02", "16");
		jedis.lpush("keylist02", "8");
		jedis.lpush("keylist02", "4");
		jedis.lpush("keylist02", "2");
		jedis.lpush("keylist02", "1");
		System.out.println("keylist02 is " + jedis.lrange("keylist02", 0, -1));
		jedis.sort("keylist02");
		System.out.println("after sort keylist02 is " + jedis.lrange("keylist02", 0, -1));
	}

	public static void main(String[] args)
	{
		Jedis jedis = getConnection("localhost", 6379);

		cleanAll(jedis);
		stringTest(jedis);
		mapTest(jedis);
		listTest(jedis);
		sortTest(jedis);
		setTest(jedis);
		jedis.close();
	}
}

Redis入门之增删改查(命令行)

1、到github上下载需要的版本,我用的是官方的windows版本。
redis下载地址

2、然后启动Redis

Set REDIS_HOME=C:\Database\Redis\Redis-x64-2.8.2103
Set PATH=%REDIS_HOME%;%PATH%

redis-server.exe D:\Database\Redis2.8\db\redis.windows.conf

配置文件redis.windows.conf中,我只修改了这两项:

logfile "D:/Database/Redis2.8/db/logs/redis_log.txt"
dir "D:/Database/Redis2.8/db/data/"

如果要停止redis

#停止Redis
redis-cli -h localhost -p 6379 shutdown

如果要查看redis信息

#查看信息
redis-cli -h localhost -p 6379 info

3、启动redis-cli.exe,连接到redis

#连接Redis
redis-cli -h localhost -p 6379

4、String类型数据操作

localhost:6379> dbsize
(integer) 0
localhost:6379> set key01 a
OK
localhost:6379> set key02 b
OK
localhost:6379> set key03 c
OK
localhost:6379> set key04 d
OK
localhost:6379> set key05 e
OK
localhost:6379> set key06 f
OK
localhost:6379> dbsize
(integer) 6
localhost:6379> set name hansen
OK
localhost:6379> keys key*
1) "key03"
2) "key04"
3) "key01"
4) "key05"
5) "key02"
6) "key06"
localhost:6379> exists key01
(integer) 1
localhost:6379> exists key07
(integer) 0
localhost:6379> get key06
"f"
localhost:6379> get name
"hansen"
localhost:6379> rename name myname
OK
localhost:6379> get myname
"hansen"
localhost:6379> get name
(nil)
localhost:6379> dbsize
(integer) 7
localhost:6379> move key06 1
(integer) 1
localhost:6379> dbsize
(integer) 6
localhost:6379> exists key06
(integer) 0
localhost:6379> select 1
OK
localhost:6379[1]> keys *
1) "key06"
localhost:6379[1]> select 0
OK
localhost:6379> exists key05
(integer) 1
localhost:6379> del key05
(integer) 1
localhost:6379> exists key05
(integer) 0

5、Map类型数据操作

localhost:6379> hset hset01 username "hansen"
(integer) 1
localhost:6379> hset hset02 username "neohope"
(integer) 1
localhost:6379> dbsize
(integer) 2
localhost:6379> hset hset01 sex "male"
(integer) 0
localhost:6379> hset hset02 sex "male"
(integer) 1
localhost:6379> dbsize
localhost:6379> type hset01
hash
(integer) 2
localhost:6379> hlen hset01
(integer) 2
localhost:6379> hkeys hset01
1) "username"
2) "sex"
localhost:6379> hget hset01 username
"hansen"

6、List类型数据操作

localhost:6379> lpush listkey01 a
(integer) 1
localhost:6379> lpush listkey01 b
(integer) 2
localhost:6379> lpush listkey01 c
(integer) 3
localhost:6379> lpush listkey01 d
(integer) 4
localhost:6379> lpush listkey01 e
(integer) 5
localhost:6379>  lrange listkey01 0 -1
1) "e"
2) "d"
3) "c"
4) "b"
5) "a"
localhost:6379> lindex listkey01 3
"b"
localhost:6379> lrem listkey01 0 c
(integer) 1
localhost:6379> lrange listkey01 0 -1
1) "e"
2) "d"
3) "b"
4) "a"
localhost:6379> lpop listkey01
"e"
localhost:6379> lpop listkey01
"d"
localhost:6379> lrange listkey01 0 -1
1) "b"
2) "a"
localhost:6379> lpush listkey02 1
(integer) 1
localhost:6379> lpush listkey02 2
(integer) 2
localhost:6379> lpush listkey02 3
(integer) 3
localhost:6379> lpush listkey02 4
(integer) 4
localhost:6379> lpush listkey02 5
(integer) 5
localhost:6379> lrange listkey02 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
localhost:6379> sort listkey02
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
localhost:6379> lrange listkey02 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
localhost:6379>

7、Set类型数据操作

localhost:6379> sadd keyset01 1 2 3
(integer) 3
localhost:6379> sadd keyset02 4 5 6
(integer) 3
localhost:6379> dbsize
(integer) 2
localhost:6379> type keyset01
set
localhost:6379> smembers keyset01
1) "1"
2) "2"
3) "3"

8、Redis常用命令(在命令行使用时,请把函数的括号及逗号都替换为空格)

连接操作相关的命令
quit 关闭连接(connection)
auth 简单密码认证
对value操作的命令
exists(key) 确认一个key是否存在
del(key) 删除一个key
type(key) 返回值的类型
keys(pattern) 返回满足给定pattern的所有key
randomkey 随机返回key空间的一个key
rename(oldname, newname) 将key由oldname重命名为newname,若newname存在则删除newname表示的key
dbsize 返回当前数据库中key的数目
expire 设定一个key的活动时间(s)
ttl 获得一个key的活动时间
select(index) 按索引查询
move(key, dbindex) 将当前数据库中的key转移到有dbindex索引的数据库
flushdb 删除当前选择数据库中的所有key
flushall 删除所有数据库中的所有key
对String操作的命令
set(key, value) 给数据库中名称为key的string赋予值value
get(key) 返回数据库中名称为key的string的value
getset(key, value) 给名称为key的string赋予上一次的value
mget(key1, key2,…, key N) 返回库中多个string(它们的名称为key1,key2…)的value
setnx(key, value) 如果不存在名称为key的string,则向库中添加string,名称为key,值为value
setex(key, time, value) 向库中添加string(名称为key,值为value)同时,设定过期时间time
mset(key1, value1, key2, value2,…key N, value N) 同时给多个string赋值,名称为key i的string赋值value i
msetnx(key1, value1, key2, value2,…key N, value N) 如果所有名称为key i的string都不存在,则向库中添加string,名称key i赋值为value i
incr(key) 名称为key的string增1操作
incrby(key, integer) 名称为key的string增加integer
decr(key) 名称为key的string减1操作
decrby(key, integer) 名称为key的string减少integer
append(key, value) 名称为key的string的值附加value
substr(key, start, end) 返回名称为key的string的value的子串
对List操作的命令
rpush(key, value) 在名称为key的list尾添加一个值为value的元素
lpush(key, value) 在名称为key的list头添加一个值为value的 元素
llen(key) 返回名称为key的list的长度
lrange(key, start, end) 返回名称为key的list中start至end之间的元素(下标从0开始,下同)
ltrim(key, start, end) 截取名称为key的list,保留start至end之间的元素
lindex(key, index) 返回名称为key的list中index位置的元素
lset(key, index, value) 给名称为key的list中index位置的元素赋值为value
lrem(key, count, value) 删除count个名称为key的list中值为value的元素。count为0,删除所有值为value的元素,count>0从头至尾删除count个值为value的元素,count<0从尾到头删除|count|个值为value的元素。
lpop(key) 返回并删除名称为key的list中的首元素 rpop(key),返回并删除名称为key的list中的尾元素
blpop(key1, key2,… key N, timeout) lpop命令的block版本。即当timeout为0时,若遇到名称为key i的list不存在或该list为空,则命令结束。如果timeout>0,则遇到上述情况时,等待timeout秒,如果问题没有解决,则对keyi+1开始的list执行pop操作。
brpop(key1, key2,… key N, timeout) rpop的block版本。参考上一命令。
rpoplpush(srckey, dstkey) 返回并删除名称为srckey的list的尾元素,并将该元素添加到名称为dstkey的list的头部
对Set操作的命令
sadd(key, member) 向名称为key的set中添加元素member
srem(key, member) 删除名称为key的set中的元素member
spop(key) 随机返回并删除名称为key的set中一个元素
smove(srckey, dstkey, member) 将member元素从名称为srckey的集合移到名称为dstkey的集合
scard(key) 返回名称为key的set的基数
sismember(key, member) 测试member是否是名称为key的set的元素
sinter(key1, key2,…key N) 求交集
sinterstore(dstkey, key1, key2,…key N) 求交集并将交集保存到dstkey的集合
sunion(key1, key2,…key N) 求并集
sunionstore(dstkey, key1, key2,…key N) 求并集并将并集保存到dstkey的集合
sdiff(key1, key2,…key N) 求差集
sdiffstore(dstkey, key1, key2,…key N) 求差集并将差集保存到dstkey的集合
smembers(key) 返回名称为key的set的所有元素
srandmember(key) 随机返回名称为key的set的一个元素
对zset(sorted set)操作的命令
zadd(key, score, member) 向名称为key的zset中添加元素member,score用于排序。如果该元素已经存在,则根据score更新该元素的顺序。
zrem(key, member) 删除名称为key的zset中的元素member
zincrby(key, increment, member) 如果在名称为key的zset中已经存在元素member,则该元素的score增加increment;否则向集合中添加该元素,其score的值为increment
zrank(key, member) 返回名称为key的zset(元素已按score从小到大排序)中member元素的rank(即index,从0开始),若没有member元素,返回“nil”
zrevrank(key, member) 返回名称为key的zset(元素已按score从大到小排序)中member元素的rank(即index,从0开始),若没有member元素,返回“nil”
zrange(key, start, end) 返回名称为key的zset(元素已按score从小到大排序)中的index从start到end的所有元素
zrevrange(key, start, end) 返回名称为key的zset(元素已按score从大到小排序)中的index从start到end的所有元素
zrangebyscore(key, min, max) 返回名称为key的zset中score >= min且score <= max的所有元素
zcard(key):返回名称为key的zset的基数 zscore(key, element) 返回名称为key的zset中元素element的
score zremrangebyrank(key, min, max) 删除名称为key的zset中rank >= min且rank <= max的所有元素
zremrangebyscore(key, min, max) 删除名称为key的zset中score >= min且score <= max的所有元素
zunionstore / zinterstore(dstkeyN, key1,…,keyN, WEIGHTS w1,…wN, AGGREGATE SUM|MIN|MAX) 对N个zset求并集和交集,并将最后的集合保存在dstkeyN中。对于集合中每一个元素的score,在进行AGGREGATE运算前,都要乘以对于的WEIGHT参数。如果没有提供WEIGHT,默认为1。默认的AGGREGATE是SUM,即结果集合中元素的score是所有集合对应元素进行SUM运算的值,而MIN和MAX是指,结果集合中元素的score是所有集合对应元素中最小值和最大值。
对Hash操作的命令
hset(key, field, value) 向名称为key的hash中添加元素field<—>value
hget(key, field) 返回名称为key的hash中field对应的value
hmget(key, field1, …,field N) 返回名称为key的hash中field i对应的value
hmset(key, field1, value1,…,field N, value N) 向名称为key的hash中添加元素field i<—>value i
hincrby(key, field, integer) 将名称为key的hash中field的value增加integer
hexists(key, field) 名称为key的hash中是否存在键为field的域
hdel(key, field) 删除名称为key的hash中键为field的域
hlen(key) 返回名称为key的hash中元素个数
hkeys(key) 返回名称为key的hash中所有键
hvals(key) 返回名称为key的hash中所有键对应的value
hgetall(key) 返回名称为key的hash中所有的键(field)及其对应的value
持久化
save 将数据同步保存到磁盘
bgsave 将数据异步保存到磁盘
lastsave 返回上次成功将数据保存到磁盘的Unix时戳
shundown 将数据同步保存到磁盘,然后关闭服务
远程服务控制
info 提供服务器的信息和统计
monitor 实时转储收到的请求
slaveof 改变复制策略设置
config 在运行时配置Redis服务器