set和get
Set带空格的字符串值,这时候就需要使用双引号了,否则会出错
getset
getset设置某个key-value,然后把设置之前的值返回来
incr,incrby 和 decr,decrby
如果值是数值的话,那么可以使用incr,表示自增。
incr key就会对该key对应的value进行自增:
默认的增量是1。
也可以指定增量,使用incrby这个命令。
格式是incrby key 增量值
相应的自减就是decr和decrby
mset 和 mget
mset一次设置多个key-value,mget就是一次获取多个key的value
exists
exists判断key对应的值是否存在,格式为exists key
返回1表示true,0表示false
del
del可以删除Key,删除的key不存在返回0
type
expire,ex,ttl
expire设置key的有效期,格式为expire key 时长(秒)
使用ex的话,可以在设置key的时候直接设置有效期,格式为set key value ex 时长(秒)
而ttl命令可以查看key还能存活多久
ttl返回-2表示该key不存在。
ttl返回-1表示key存在,但是没有设置expire。
ttl返回非负数表示剩余的存活时长(秒)。
在.NET Core 项目中操练String
set,get,getset
namespace ConsoleTest { class Program { static void Main(string[] args) { ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect("120.132.116.153:6379"); IDatabase database = connectionMultiplexer.GetDatabase(0); database.StringSet("name", "wolf"); Console.WriteLine($"{database.StringGet("name")}"); Console.WriteLine($"{database.StringGetSet("name","wolf2")}"); Console.WriteLine($"{database.StringGet("name")}"); Console.ReadLine(); } } }
incr,incrby,decr,decrby
class Program { static void Main(string[] args) { ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect("120.132.116.153:6379"); IDatabase database = connectionMultiplexer.GetDatabase(0); database.StringSet("num", 0); Console.WriteLine($"{database.StringGet("num")}"); Console.WriteLine($"{database.StringIncrement("num")}"); Console.WriteLine($"{database.StringIncrement("num",10)}"); Console.WriteLine($"{database.StringDecrement("num")}"); Console.WriteLine($"{database.StringDecrement("num", 5)}"); Console.ReadLine(); } }
mset,mget
class Program { static void Main(string[] args) { ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect("120.132.116.153:6379"); IDatabase database = connectionMultiplexer.GetDatabase(0); database.StringSet(new KeyValuePair<RedisKey, RedisValue>[] { new KeyValuePair<RedisKey, RedisValue>("n1","b1"), new KeyValuePair<RedisKey, RedisValue>("n2","b2"), new KeyValuePair<RedisKey, RedisValue>("n3","b3"), }); var values = database.StringGet(new RedisKey[] {"n1", "n2", "n3"}); foreach (var item in values) { Console.WriteLine($"{item}"); } Console.ReadLine(); } }
exists,del
class Program { static void Main(string[] args) { ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect("120.132.116.153:6379"); IDatabase database = connectionMultiplexer.GetDatabase(0); database.StringSet("name", "wolf"); Console.WriteLine($"{database.KeyExists("name")}"); Console.WriteLine($"{database.KeyDelete("name")}"); Console.WriteLine($"{database.KeyDelete("name123")}"); Console.WriteLine($"{database.KeyExists("name")}"); Console.ReadLine(); } }
ex,expire,ttl
class Program { static void Main(string[] args) { ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect("120.132.116.153:6379"); IDatabase database = connectionMultiplexer.GetDatabase(0); database.StringSet("name", "wolf", TimeSpan.FromSeconds(2)); Console.WriteLine($"{database.StringGet("name")}"); Thread.Sleep(2 * 1000); Console.WriteLine($"after {database.StringGet("name")}"); database.StringSet("name", "wolf2"); database.KeyExpire("name", TimeSpan.FromSeconds(2)); Console.WriteLine($"{database.StringGet("name")}"); Thread.Sleep(2 * 1000); Console.WriteLine($"after {database.StringGet("name")}"); database.StringSet("name", "wolf", TimeSpan.FromSeconds(2)); Thread.Sleep(1000); Console.WriteLine($"TTl:{database.KeyIdleTime("name")}"); Console.ReadLine(); } }