|
@@ -1,6 +1,5 @@
|
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json;
|
|
|
using StackExchange.Redis;
|
|
using StackExchange.Redis;
|
|
|
-using System.Runtime.Serialization.Formatters.Binary;
|
|
|
|
|
using YSAI.Core.@interface;
|
|
using YSAI.Core.@interface;
|
|
|
using YSAI.Unility;
|
|
using YSAI.Unility;
|
|
|
|
|
|
|
@@ -131,7 +130,6 @@ namespace YSAI.Core.redis
|
|
|
//序列化类型
|
|
//序列化类型
|
|
|
public enum SerializeType
|
|
public enum SerializeType
|
|
|
{
|
|
{
|
|
|
- Binary, //二进制
|
|
|
|
|
Json, //Json
|
|
Json, //Json
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -221,10 +219,6 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
switch (serializeType)
|
|
switch (serializeType)
|
|
|
{
|
|
{
|
|
|
- case SerializeType.Binary:
|
|
|
|
|
- var binaryValue = Serialize(redisValue);
|
|
|
|
|
- return _DB.StringSet(basics.Prefix + redisKey, binaryValue, expiry);
|
|
|
|
|
-
|
|
|
|
|
case SerializeType.Json:
|
|
case SerializeType.Json:
|
|
|
var jsonValue = SerializeJson(redisValue);
|
|
var jsonValue = SerializeJson(redisValue);
|
|
|
return _DB.StringSet(basics.Prefix + redisKey, jsonValue, expiry);
|
|
return _DB.StringSet(basics.Prefix + redisKey, jsonValue, expiry);
|
|
@@ -253,9 +247,6 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
switch (serializeType)
|
|
switch (serializeType)
|
|
|
{
|
|
{
|
|
|
- case SerializeType.Binary:
|
|
|
|
|
- return Deserialize<T>(_DB.StringGet(basics.Prefix + redisKey));
|
|
|
|
|
-
|
|
|
|
|
case SerializeType.Json:
|
|
case SerializeType.Json:
|
|
|
return DeserializeJson<T>(_DB.StringGet(basics.Prefix + redisKey));
|
|
return DeserializeJson<T>(_DB.StringGet(basics.Prefix + redisKey));
|
|
|
|
|
|
|
@@ -344,10 +335,6 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
switch (serializeType)
|
|
switch (serializeType)
|
|
|
{
|
|
{
|
|
|
- case SerializeType.Binary:
|
|
|
|
|
- var binaryValue = Serialize(redisValue);
|
|
|
|
|
- return await _DB.StringSetAsync(basics.Prefix + redisKey, binaryValue, expiry);
|
|
|
|
|
-
|
|
|
|
|
case SerializeType.Json:
|
|
case SerializeType.Json:
|
|
|
var jsonValue = SerializeJson(redisValue);
|
|
var jsonValue = SerializeJson(redisValue);
|
|
|
return await _DB.StringSetAsync(basics.Prefix + redisKey, jsonValue, expiry);
|
|
return await _DB.StringSetAsync(basics.Prefix + redisKey, jsonValue, expiry);
|
|
@@ -377,9 +364,6 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
switch (serializeType)
|
|
switch (serializeType)
|
|
|
{
|
|
{
|
|
|
- case SerializeType.Binary:
|
|
|
|
|
- return Deserialize<T>(await _DB.StringGetAsync(basics.Prefix + redisKey));
|
|
|
|
|
-
|
|
|
|
|
case SerializeType.Json:
|
|
case SerializeType.Json:
|
|
|
return DeserializeJson<T>(await _DB.StringGetAsync(basics.Prefix + redisKey));
|
|
return DeserializeJson<T>(await _DB.StringGetAsync(basics.Prefix + redisKey));
|
|
|
|
|
|
|
@@ -579,10 +563,6 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
switch (serializeType)
|
|
switch (serializeType)
|
|
|
{
|
|
{
|
|
|
- case SerializeType.Binary:
|
|
|
|
|
- var binaryValue = Serialize(redisValue);
|
|
|
|
|
- return _DB.HashSet(basics.Prefix + redisKey, hashField, binaryValue);
|
|
|
|
|
-
|
|
|
|
|
case SerializeType.Json:
|
|
case SerializeType.Json:
|
|
|
var jsonValue = SerializeJson(redisValue);
|
|
var jsonValue = SerializeJson(redisValue);
|
|
|
return _DB.HashSet(basics.Prefix + redisKey, hashField, jsonValue);
|
|
return _DB.HashSet(basics.Prefix + redisKey, hashField, jsonValue);
|
|
@@ -611,9 +591,6 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
switch (serializeType)
|
|
switch (serializeType)
|
|
|
{
|
|
{
|
|
|
- case SerializeType.Binary:
|
|
|
|
|
- return Deserialize<T>(_DB.HashGet(basics.Prefix + redisKey, hashField));
|
|
|
|
|
-
|
|
|
|
|
case SerializeType.Json:
|
|
case SerializeType.Json:
|
|
|
return DeserializeJson<T>(_DB.HashGet(basics.Prefix + redisKey, hashField));
|
|
return DeserializeJson<T>(_DB.HashGet(basics.Prefix + redisKey, hashField));
|
|
|
|
|
|
|
@@ -813,10 +790,6 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
switch (serializeType)
|
|
switch (serializeType)
|
|
|
{
|
|
{
|
|
|
- case SerializeType.Binary:
|
|
|
|
|
- var binaryValue = Serialize(value);
|
|
|
|
|
- return await _DB.HashSetAsync(basics.Prefix + redisKey, hashField, binaryValue);
|
|
|
|
|
-
|
|
|
|
|
case SerializeType.Json:
|
|
case SerializeType.Json:
|
|
|
var jsonValue = SerializeJson(value);
|
|
var jsonValue = SerializeJson(value);
|
|
|
return await _DB.HashSetAsync(basics.Prefix + redisKey, hashField, jsonValue);
|
|
return await _DB.HashSetAsync(basics.Prefix + redisKey, hashField, jsonValue);
|
|
@@ -845,9 +818,6 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
switch (serializeType)
|
|
switch (serializeType)
|
|
|
{
|
|
{
|
|
|
- case SerializeType.Binary:
|
|
|
|
|
- return Deserialize<T>(await _DB.HashGetAsync(basics.Prefix + redisKey, hashField));
|
|
|
|
|
-
|
|
|
|
|
case SerializeType.Json:
|
|
case SerializeType.Json:
|
|
|
return DeserializeJson<T>(await _DB.HashGetAsync(basics.Prefix + redisKey, hashField));
|
|
return DeserializeJson<T>(await _DB.HashGetAsync(basics.Prefix + redisKey, hashField));
|
|
|
|
|
|
|
@@ -1005,9 +975,6 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
switch (serializeType)
|
|
switch (serializeType)
|
|
|
{
|
|
{
|
|
|
- case SerializeType.Binary:
|
|
|
|
|
- return Deserialize<T>(_DB.ListLeftPop(basics.Prefix + redisKey));
|
|
|
|
|
-
|
|
|
|
|
case SerializeType.Json:
|
|
case SerializeType.Json:
|
|
|
return DeserializeJson<T>(_DB.ListLeftPop(basics.Prefix + redisKey));
|
|
return DeserializeJson<T>(_DB.ListLeftPop(basics.Prefix + redisKey));
|
|
|
|
|
|
|
@@ -1033,9 +1000,6 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
switch (serializeType)
|
|
switch (serializeType)
|
|
|
{
|
|
{
|
|
|
- case SerializeType.Binary:
|
|
|
|
|
- return Deserialize<T>(_DB.ListRightPop(basics.Prefix + redisKey));
|
|
|
|
|
-
|
|
|
|
|
case SerializeType.Json:
|
|
case SerializeType.Json:
|
|
|
return DeserializeJson<T>(_DB.ListRightPop(basics.Prefix + redisKey));
|
|
return DeserializeJson<T>(_DB.ListRightPop(basics.Prefix + redisKey));
|
|
|
|
|
|
|
@@ -1059,7 +1023,7 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
if (ConnetStatus)
|
|
if (ConnetStatus)
|
|
|
{
|
|
{
|
|
|
- return _DB.ListRightPush(basics.Prefix + redisKey, Serialize(redisValue));
|
|
|
|
|
|
|
+ return _DB.ListRightPush(basics.Prefix + redisKey, SerializeJson(redisValue));
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
@@ -1077,7 +1041,7 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
if (ConnetStatus)
|
|
if (ConnetStatus)
|
|
|
{
|
|
{
|
|
|
- return _DB.ListLeftPush(basics.Prefix + redisKey, Serialize(redisValue));
|
|
|
|
|
|
|
+ return _DB.ListLeftPush(basics.Prefix + redisKey, SerializeJson(redisValue));
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
@@ -1226,9 +1190,6 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
switch (serializeType)
|
|
switch (serializeType)
|
|
|
{
|
|
{
|
|
|
- case SerializeType.Binary:
|
|
|
|
|
- return Deserialize<T>(await _DB.ListLeftPopAsync(basics.Prefix + redisKey));
|
|
|
|
|
-
|
|
|
|
|
case SerializeType.Json:
|
|
case SerializeType.Json:
|
|
|
return DeserializeJson<T>(await _DB.ListLeftPopAsync(basics.Prefix + redisKey));
|
|
return DeserializeJson<T>(await _DB.ListLeftPopAsync(basics.Prefix + redisKey));
|
|
|
|
|
|
|
@@ -1254,9 +1215,6 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
switch (serializeType)
|
|
switch (serializeType)
|
|
|
{
|
|
{
|
|
|
- case SerializeType.Binary:
|
|
|
|
|
- return Deserialize<T>(await _DB.ListRightPopAsync(basics.Prefix + redisKey));
|
|
|
|
|
-
|
|
|
|
|
case SerializeType.Json:
|
|
case SerializeType.Json:
|
|
|
return DeserializeJson<T>(await _DB.ListRightPopAsync(basics.Prefix + redisKey));
|
|
return DeserializeJson<T>(await _DB.ListRightPopAsync(basics.Prefix + redisKey));
|
|
|
|
|
|
|
@@ -1280,7 +1238,7 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
if (ConnetStatus)
|
|
if (ConnetStatus)
|
|
|
{
|
|
{
|
|
|
- return await _DB.ListRightPushAsync(basics.Prefix + redisKey, Serialize(redisValue));
|
|
|
|
|
|
|
+ return await _DB.ListRightPushAsync(basics.Prefix + redisKey, SerializeJson(redisValue));
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
@@ -1298,7 +1256,7 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
if (ConnetStatus)
|
|
if (ConnetStatus)
|
|
|
{
|
|
{
|
|
|
- return await _DB.ListLeftPushAsync(basics.Prefix + redisKey, Serialize(redisValue));
|
|
|
|
|
|
|
+ return await _DB.ListLeftPushAsync(basics.Prefix + redisKey, SerializeJson(redisValue));
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
@@ -1400,7 +1358,7 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
if (ConnetStatus)
|
|
if (ConnetStatus)
|
|
|
{
|
|
{
|
|
|
- var json = Serialize(member);
|
|
|
|
|
|
|
+ var json = SerializeJson(member);
|
|
|
return _DB.SortedSetAdd(basics.Prefix + redisKey, json, score);
|
|
return _DB.SortedSetAdd(basics.Prefix + redisKey, json, score);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
@@ -1514,7 +1472,7 @@ namespace YSAI.Core.redis
|
|
|
{
|
|
{
|
|
|
if (ConnetStatus)
|
|
if (ConnetStatus)
|
|
|
{
|
|
{
|
|
|
- var json = Serialize(member);
|
|
|
|
|
|
|
+ var json = SerializeJson(member);
|
|
|
return await _DB.SortedSetAddAsync(basics.Prefix + redisKey, json, score);
|
|
return await _DB.SortedSetAddAsync(basics.Prefix + redisKey, json, score);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
@@ -1792,7 +1750,7 @@ namespace YSAI.Core.redis
|
|
|
public long Publish<T>(RedisChannel channel, T message)
|
|
public long Publish<T>(RedisChannel channel, T message)
|
|
|
{
|
|
{
|
|
|
var sub = _ConnMultiplexer.GetSubscriber();
|
|
var sub = _ConnMultiplexer.GetSubscriber();
|
|
|
- return sub.Publish(channel, Serialize(message));
|
|
|
|
|
|
|
+ return sub.Publish(channel, SerializeJson(message));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#endregion 同步方式
|
|
#endregion 同步方式
|
|
@@ -1832,7 +1790,7 @@ namespace YSAI.Core.redis
|
|
|
public async Task<long> PublishAsync<T>(RedisChannel channel, T message)
|
|
public async Task<long> PublishAsync<T>(RedisChannel channel, T message)
|
|
|
{
|
|
{
|
|
|
var sub = _ConnMultiplexer.GetSubscriber();
|
|
var sub = _ConnMultiplexer.GetSubscriber();
|
|
|
- return await sub.PublishAsync(channel, Serialize(message));
|
|
|
|
|
|
|
+ return await sub.PublishAsync(channel, SerializeJson(message));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#endregion 异步方式
|
|
#endregion 异步方式
|
|
@@ -1962,44 +1920,6 @@ namespace YSAI.Core.redis
|
|
|
return list.Select(x => x.ToString());
|
|
return list.Select(x => x.ToString());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// 序列化(二进制)
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- /// <param name="obj"></param>
|
|
|
|
|
- /// <returns></returns>
|
|
|
|
|
- private static byte[] Serialize(object obj)
|
|
|
|
|
- {
|
|
|
|
|
- if (obj == null)
|
|
|
|
|
- return null;
|
|
|
|
|
-
|
|
|
|
|
- var binaryFormatter = new BinaryFormatter();
|
|
|
|
|
- using (var memoryStream = new MemoryStream())
|
|
|
|
|
- {
|
|
|
|
|
- binaryFormatter.Serialize(memoryStream, obj);
|
|
|
|
|
- var data = memoryStream.ToArray();
|
|
|
|
|
- return data;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// 反序列化(二进制)
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- /// <typeparam name="T"></typeparam>
|
|
|
|
|
- /// <param name="data"></param>
|
|
|
|
|
- /// <returns></returns>
|
|
|
|
|
- private static T Deserialize<T>(byte[] data)
|
|
|
|
|
- {
|
|
|
|
|
- if (data == null)
|
|
|
|
|
- return default(T);
|
|
|
|
|
-
|
|
|
|
|
- var binaryFormatter = new BinaryFormatter();
|
|
|
|
|
- using (var memoryStream = new MemoryStream(data))
|
|
|
|
|
- {
|
|
|
|
|
- var result = (T)binaryFormatter.Deserialize(memoryStream);
|
|
|
|
|
- return result;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
/// 序列化对象为Json字符串
|
|
/// 序列化对象为Json字符串
|
|
|
/// </summary>
|
|
/// </summary>
|