|
|
|
@ -4,6 +4,9 @@ using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
|
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
using Xunit;
|
|
|
|
|
using Xunit.Extensions;
|
|
|
|
@ -40,12 +43,152 @@ namespace RedisStudyTest
|
|
|
|
|
|
|
|
|
|
#region StringSet
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 键名为空串时,操作正常
|
|
|
|
|
/// 只是"Redis Desktop Manager"管理工具显示空白
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void StringSet()
|
|
|
|
|
public void StringSetKeyIsEmptyTest()
|
|
|
|
|
{
|
|
|
|
|
//添加正常
|
|
|
|
|
var setResult = redisDatabase.StringSet(string.Empty, "Redis String key is string.empty");
|
|
|
|
|
Assert.True(setResult);
|
|
|
|
|
|
|
|
|
|
var getValue = redisDatabase.StringGet(string.Empty);
|
|
|
|
|
Assert.Equal("Redis String key is string.empty", getValue);
|
|
|
|
|
|
|
|
|
|
//更新正常
|
|
|
|
|
setResult = redisDatabase.StringSet(string.Empty, "Updage empty key value to new values");
|
|
|
|
|
Assert.True(setResult);
|
|
|
|
|
|
|
|
|
|
getValue = redisDatabase.StringGet(string.Empty);
|
|
|
|
|
Assert.Equal("Updage empty key value to new values", getValue);
|
|
|
|
|
|
|
|
|
|
//清理
|
|
|
|
|
redisDatabase.KeyDelete(string.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置一个键值对
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void StringSetOneKeyValueTest()
|
|
|
|
|
{
|
|
|
|
|
//不存在时,添加
|
|
|
|
|
var setResult = redisStringStudy.StringSet(defaultRedisKey, "xUnit", defaultExpiry);
|
|
|
|
|
Assert.True(setResult);
|
|
|
|
|
|
|
|
|
|
var redisValue = redisStringStudy.StringGet(defaultRedisKey);
|
|
|
|
|
Assert.Equal("xUnit", redisValue);
|
|
|
|
|
|
|
|
|
|
//存在时,更新
|
|
|
|
|
var setResult2 = redisStringStudy.StringSet(defaultRedisKey, "xUnit update", defaultExpiry);
|
|
|
|
|
Assert.True(setResult2);
|
|
|
|
|
|
|
|
|
|
var redisValue2 = redisStringStudy.StringGet(defaultRedisKey);
|
|
|
|
|
Assert.Equal("xUnit update", redisValue2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置值为字符串
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void StringSetForString()
|
|
|
|
|
{
|
|
|
|
|
var setResult = redisStringStudy.StringSet(defaultRedisKey, "I am string", defaultExpiry);
|
|
|
|
|
Assert.True(setResult);
|
|
|
|
|
|
|
|
|
|
var redisValue = redisStringStudy.StringGet(defaultRedisKey);
|
|
|
|
|
Assert.Equal("I am string", redisValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置值为数字
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void StringSetForNumber()
|
|
|
|
|
{
|
|
|
|
|
var setResult = redisStringStudy.StringSet(defaultRedisKey, 20000, defaultExpiry);
|
|
|
|
|
Assert.True(setResult);
|
|
|
|
|
|
|
|
|
|
var redisValue = redisStringStudy.StringGet(defaultRedisKey);
|
|
|
|
|
Assert.Equal(20000, redisValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置值为json
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void StringSetForJson()
|
|
|
|
|
{
|
|
|
|
|
var stuent = new
|
|
|
|
|
{
|
|
|
|
|
Id=1,
|
|
|
|
|
Name="王高峰",
|
|
|
|
|
Age=30,
|
|
|
|
|
Number="05121098"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var json = JsonConvert.SerializeObject(stuent);
|
|
|
|
|
|
|
|
|
|
var setResult = redisStringStudy.StringSet(defaultRedisKey, json, defaultExpiry);
|
|
|
|
|
Assert.True(setResult);
|
|
|
|
|
|
|
|
|
|
//Newtonsoft.Json 反序列化为匿名类型
|
|
|
|
|
var redisValue = redisStringStudy.StringGet(defaultRedisKey);
|
|
|
|
|
var anonymousType = JsonConvert.DeserializeAnonymousType(redisValue, stuent);
|
|
|
|
|
Assert.True(anonymousType.Id == stuent.Id && anonymousType.Name == stuent.Name && anonymousType.Age == stuent.Age && anonymousType.Number == stuent.Number);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置值为Bit
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void StringSetForBit()
|
|
|
|
|
{
|
|
|
|
|
var setResult = redisStringStudy.StringSet(defaultRedisKey, Encoding.UTF8.GetBytes("我要转变为二进制数据,再存储"), defaultExpiry);
|
|
|
|
|
Assert.True(setResult);
|
|
|
|
|
|
|
|
|
|
var redisValue = (byte[])redisStringStudy.StringGet(defaultRedisKey);
|
|
|
|
|
|
|
|
|
|
Assert.Equal("我要转变为二进制数据,再存储", Encoding.UTF8.GetString(redisValue, 0, redisValue.Length));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置一组键值对
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void StringSetGroupKeyValueTest()
|
|
|
|
|
{
|
|
|
|
|
//添加
|
|
|
|
|
KeyValuePair<RedisKey, RedisValue>[] keyValuePairs = new KeyValuePair<RedisKey, RedisValue>[]
|
|
|
|
|
{
|
|
|
|
|
new KeyValuePair<RedisKey, RedisValue>("RedisStudy:String:1","group1"),
|
|
|
|
|
new KeyValuePair<RedisKey, RedisValue>("RedisStudy:String:2",20),
|
|
|
|
|
new KeyValuePair<RedisKey, RedisValue>("RedisStudy:String:3","{\"Id\":1,\"Name\":\"wanggaofeng\",\"Age\":20}"),
|
|
|
|
|
new KeyValuePair<RedisKey, RedisValue>("RedisStudy:String:3",BitConverter.GetBytes(123)),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var setResult = redisStringStudy.StringSet(keyValuePairs);
|
|
|
|
|
Assert.True(setResult);
|
|
|
|
|
|
|
|
|
|
//更新
|
|
|
|
|
keyValuePairs = new KeyValuePair<RedisKey, RedisValue>[]
|
|
|
|
|
{
|
|
|
|
|
new KeyValuePair<RedisKey, RedisValue>("RedisStudy:String:1","group3"),
|
|
|
|
|
new KeyValuePair<RedisKey, RedisValue>("RedisStudy:String:2",20),
|
|
|
|
|
new KeyValuePair<RedisKey, RedisValue>("RedisStudy:String:3","{\"Id\":1,\"Name\":\"wanggaofeng\",\"Age\":20}"),
|
|
|
|
|
new KeyValuePair<RedisKey, RedisValue>("RedisStudy:String:3",BitConverter.GetBytes(200)),
|
|
|
|
|
};
|
|
|
|
|
setResult = redisStringStudy.StringSet(keyValuePairs);
|
|
|
|
|
Assert.True(setResult);
|
|
|
|
|
|
|
|
|
|
//清理
|
|
|
|
|
redisDatabase.KeyDelete("RedisStudy:String:1");
|
|
|
|
|
redisDatabase.KeyDelete("RedisStudy:String:2");
|
|
|
|
|
redisDatabase.KeyDelete("RedisStudy:String:3");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|