/// <summary> /// 异步移除有序集合key里的value值 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="sstyle"></param> public static Task <bool> SortSetRemoveAsync <T>(string key, T value, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { return SortSetRemove(key, value, sstyle); })); }
/// <summary> /// 异步根据索引的起始终止值获取有序递增排列集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="start"></param> /// <param name="end"></param> /// <param name="sstyle"></param> /// <returns></returns> public static List <T> SortSetRange <T>(string key, long start, long end, SerializationStyle sstyle = SerializationStyle.Binary) { RedisValue[] vals; vals = CacheDatabase.SortedSetRangeByRank(key, start, end); if (vals == null || !vals.Any()) { return(new List <T>()); } List <T> results = new List <T>(); if (sstyle == SerializationStyle.Json) { foreach (RedisValue val in vals) { results.Add(JsonConvert.DeserializeObject <T>(val)); } } else { foreach (RedisValue val in vals) { results.Add(Deserialize <T>(val)); } } return(results); }
/// <summary> /// 设置缓存 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="ts"></param> /// <param name="sstyle"></param> public static Task <bool> SetAsync <T>(string key, T value, TimeSpan ts, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { return Set(key, value, ts, sstyle); })); }
/// <summary> /// 异步返回集合的所有元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="sstyle"></param> /// <returns></returns> public static Task <List <T> > SortSetALLReadAsync <T>(string key, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { List <T> result = new List <T>(); RedisValue[] vals; vals = CacheDatabase.SortedSetRangeByRank(key); if (vals == null || !vals.Any()) { return new List <T>(); } if (sstyle == SerializationStyle.Json) { foreach (RedisValue val in vals) { result.Add(JsonConvert.DeserializeObject <T>(val)); } } else { foreach (var val in vals) { result.Add(Deserialize <T>(val)); } } return result; })); }
/// <summary> /// 异步获取有序递增排列集合所有数据并删除 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="sstyle"></param> /// <returns></returns> public static Task <List <T> > SortSetALLReadRemoveAsync <T>(string key, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { RedisValue[] vals; vals = CacheDatabase.SortedSetRangeByRank(key); if (vals != null && vals.Any()) { // 移除集合中的数据 CacheDatabase.SortedSetRemoveRangeByRank(key, 0, vals.Length); // 从所有redis的key有序集合中删除键 ListRemoveAsync(redisallkeys, key); } if (vals == null || !vals.Any()) { return new List <T>(); } List <T> results = new List <T>(); if (sstyle == SerializationStyle.Json) { foreach (RedisValue val in vals) { results.Add(JsonConvert.DeserializeObject <T>(val)); } } else { foreach (RedisValue val in vals) { results.Add(Deserialize <T>(val)); } } return results; })); }
/// <summary> /// 异步将一个或多个值插入到列表的顶部 /// </summary> /// <param name="key"></param> /// <param name="values"></param> /// <param name="sstyle"></param> /// <returns></returns> public static Task <bool> ListAddTopAsync <T>(string key, T[] values, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { if (sstyle == SerializationStyle.Json) { foreach (object value in values) { RedisValue val = JsonConvert.SerializeObject(value); CacheDatabase.ListLeftPush(key, val); } } else { foreach (object value in values) { RedisValue val = Serialize(value); CacheDatabase.ListLeftPush(key, val); } } // 添加到所有redis的key有序集合中 if (key != redisallkeys) { // 添加到所有redis的key有序集合中 ListAddAsync(redisallkeys, key); } return true; })); }
public static XmlDocument Serialize(EntityCollection collection, SerializationStyle style = SerializationStyle.Basic) { var result = new XmlDocument(); XmlNode root = result.CreateNode(XmlNodeType.Element, "Entities", ""); var entityname = result.CreateAttribute("EntityName"); entityname.Value = collection.EntityName; root.Attributes.Append(entityname); var more = result.CreateAttribute("MoreRecords"); more.Value = collection.MoreRecords.ToString(); root.Attributes.Append(more); var total = result.CreateAttribute("TotalRecordCount"); total.Value = collection.TotalRecordCount.ToString(); root.Attributes.Append(total); var paging = result.CreateAttribute("PagingCookie"); paging.Value = collection.PagingCookie; root.Attributes.Append(paging); foreach (var entity in collection.Entities) { EntitySerializer.Serialize(entity, root, style); } result.AppendChild(root); return(result); }
public void Save(string filename, SerializationStyle serializationStyle = SerializationStyle.ProtoBufNet) { SetupModel(); if (!Directory.Exists(baseSavePath)) { Directory.CreateDirectory(baseSavePath); } string filepath = Path.Combine(baseSavePath, filename); try { using (FileStream fileStream = File.Create(filepath)) { if (serializationStyle == SerializationStyle.DotNet) { SaveOldStyle(fileStream, filepath); return; } Serializer.Serialize <Replay>(fileStream, this); } } catch (Exception e) { Debug.LogError("Failed to serialize. Reason: " + e.Message); throw; } }
/// <summary> /// 异步获取的无序集合的所有元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="sstyle"></param> /// <returns></returns> public static Task <List <T> > GetMembersAsync <T>(string key, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { return GetMembers <T>(key, sstyle); })); }
private static XmlNode GetEntityNode(Entity entity, XmlDocument result, SerializationStyle style) { switch (style) { case SerializationStyle.Basic: { XmlNode xEntity = result.CreateElement("Entity"); XmlAttribute xEntityName = result.CreateAttribute("name"); xEntityName.Value = entity.LogicalName; xEntity.Attributes.Append(xEntityName); XmlAttribute xEntityId = result.CreateAttribute("id"); xEntityId.Value = entity.Id.ToString(); xEntity.Attributes.Append(xEntityId); return(xEntity); } case SerializationStyle.Explicit: { XmlNode xEntity = result.CreateElement(entity.LogicalName); XmlAttribute xEntityId = result.CreateAttribute("id"); xEntityId.Value = entity.Id.ToString(); xEntity.Attributes.Append(xEntityId); return(xEntity); } default: return(null); } }
/// <summary> /// 异步设置添加到有序集合 /// </summary> /// <param name="key"></param> /// <param name="values"></param> /// <param name="sstyle"></param> public static Task <bool> SortSetAddArrayAsync <T>(string key, T[] values, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { foreach (var value in values) { SortSetAdd(key, value, sstyle); } return true; })); }
/// <summary> /// 异步根据索引值的起始终止值删除有序递增排列集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="values"></param> /// <param name="sstyle"></param> /// <returns></returns> public static Task <bool> SortSetResetAsync <T>(string key, List <T> values, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { //删除有序集合中所有元素 CacheDatabase.SortedSetRemoveRangeByValue(key, 0, -1); foreach (var value in values) { SortSetAdd(key, value, sstyle); } return true; })); }
/// <summary> /// 返回有序集合中指定成员的分数值 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="sstyle"></param> public static double?SortSetScore <T>(string key, T value, SerializationStyle sstyle = SerializationStyle.Binary) { RedisValue val; if (sstyle == SerializationStyle.Json) { val = JsonConvert.SerializeObject(value); } else { val = Serialize(value); } return(CacheDatabase.SortedSetScore(key, val)); }
/// <summary> /// 设置添加到有序集合 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="score"></param> /// <param name="sstyle"></param> public static bool SortSetAdd <T>(string key, T value, int score, SerializationStyle sstyle = SerializationStyle.Binary) { RedisValue val; if (sstyle == SerializationStyle.Json) { val = JsonConvert.SerializeObject(value); } else { val = Serialize(value); } // 添加到所有redis的key有序集合中 ListAddAsync(redisallkeys, key); return(CacheDatabase.SortedSetAdd(key, val, score)); }
/// <summary> /// 移除集合中的一个或多个成员元素,不存在的成员元素会被忽略。 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="sstyle"></param> /// <returns></returns> public static Task <bool> SetRemoveAsync <T>(string key, T value, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { RedisValue val; if (sstyle == SerializationStyle.Json) { val = JsonConvert.SerializeObject(value); } else { val = Serialize(value); } return CacheDatabase.SetRemove(key, val); })); }
/// <summary> /// 设置缓存 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="ts"></param> /// <param name="sstyle"></param> public static bool Set <T>(string key, T value, TimeSpan ts, SerializationStyle sstyle = SerializationStyle.Binary) { RedisValue val; if (sstyle == SerializationStyle.Json) { val = JsonConvert.SerializeObject(value); } else { val = Serialize(value); } // 添加到所有redis的key有序集合中 ListAddAsync(redisallkeys, key); return(CacheDatabase.StringSet(key, val, ts)); }
/// <summary> /// 根据key获取缓存对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="sstyle"></param> /// <returns></returns> public static T Get <T>(string key, SerializationStyle sstyle = SerializationStyle.Binary) { if (string.IsNullOrEmpty(key)) { return(default(T)); } RedisValue val; val = CacheDatabase.StringGet(key); if (!val.HasValue) { return(default(T)); } if (sstyle == SerializationStyle.Json) { return(JsonConvert.DeserializeObject <T>(val)); } return(Deserialize <T>(val)); }
public static Replay ImportFromFile(string filePath, SerializationStyle serializationStyle = SerializationStyle.DotNet) { SetupModel(); using (var file = File.Open(filePath, FileMode.Open)) { if (serializationStyle == SerializationStyle.DotNet) { return(LoadOldStyle(file)); } try { return(Serializer.Deserialize <Replay>(file)); } catch (SerializationException e) { Debug.Log("Failed to deserialize. Reason: " + e.Message); throw; } } }
public static Replay ImportFromTextAsset(TextAsset replay, SerializationStyle serializationStyle = SerializationStyle.DotNet) { SetupModel(); using (MemoryStream ms = new MemoryStream(replay.bytes)) { if (serializationStyle == SerializationStyle.DotNet) { return(LoadOldStyle(ms)); } try { return(Serializer.Deserialize <Replay>(ms)); } catch (SerializationException e) { Debug.Log("Failed to deserialize. Reason: " + e.Message); throw; } } }
/// <summary> /// 异步通过索引获取列表中的元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="sstyle"></param> /// <returns></returns> public static Task <T> ListIndexAsync <T>(string key, long index, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { RedisValue val; val = CacheDatabase.ListGetByIndex(key, index); if (!val.HasValue) { return default(T); } if (sstyle == SerializationStyle.Json) { return JsonConvert.DeserializeObject <T>(val); } else { return Deserialize <T>(val); } })); }
/// <summary> /// 异步将一个或多个值插入到列表的尾部 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="sstyle"></param> /// <returns></returns> public static Task <bool> ListAddAsync <T>(string key, T value, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { RedisValue val; if (sstyle == SerializationStyle.Json) { val = JsonConvert.SerializeObject(value); } else { val = Serialize(value); } // 添加到所有redis的key有序集合中 if (key != redisallkeys) { // 添加到所有redis的key有序集合中 ListAddAsync(redisallkeys, key); } return CacheDatabase.ListRightPush(key, val) > 0; })); }
public static XmlDocument Serialize(EntityCollection collection, SerializationStyle style = SerializationStyle.Basic) { var result = new XmlDocument(); XmlNode root = result.CreateNode(XmlNodeType.Element, "Entities", ""); var entityname = result.CreateAttribute("EntityName"); entityname.Value = collection.EntityName; root.Attributes.Append(entityname); var more = result.CreateAttribute("MoreRecords"); more.Value = collection.MoreRecords.ToString(); root.Attributes.Append(more); var total = result.CreateAttribute("TotalRecordCount"); total.Value = collection.TotalRecordCount.ToString(); root.Attributes.Append(total); var paging = result.CreateAttribute("PagingCookie"); paging.Value = collection.PagingCookie; root.Attributes.Append(paging); foreach (var entity in collection.Entities) { EntitySerializer.Serialize(entity, root, style); } result.AppendChild(root); return result; }
/// <summary> /// 移除集合中的一个或多个成员元素,不存在的成员元素会被忽略。 /// </summary> /// <param name="key"></param> /// <param name="values"></param> /// <param name="sstyle"></param> /// <returns></returns> public static Task <bool> SetRemoveAsync <T>(string key, T[] values, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { if (sstyle == SerializationStyle.Json) { foreach (object value in values) { RedisValue val = JsonConvert.SerializeObject(value); CacheDatabase.SetRemove(key, val); } } else { foreach (object value in values) { RedisValue val = Serialize(value); CacheDatabase.SetRemove(key, val); } } return true; })); }
/// <summary> /// 获取key对应的无序集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="sstyle"></param> /// <returns></returns> public static List <T> GetMembers <T>(string key, SerializationStyle sstyle = SerializationStyle.Binary) { List <T> results = new List <T>(); RedisValue[] vals; vals = CacheDatabase.SetMembers(key); if (vals == null || !vals.Any()) { return(new List <T>()); } foreach (RedisValue val in vals) { if (sstyle == SerializationStyle.Json) { results.Add(JsonConvert.DeserializeObject <T>(val)); } else { results.Add(Deserialize <T>(val)); } } return(results); }
/// <summary> /// 设置集合移动 /// </summary> /// <param name="sourcekey">原始key</param> /// <param name="destinationkey">目标key</param> /// <param name="value"></param> /// <param name="sstyle"></param> public static bool SetMove <T>(string sourcekey, string destinationkey, T value, SerializationStyle sstyle = SerializationStyle.Binary) { RedisValue val; if (sstyle == SerializationStyle.Json) { val = JsonConvert.SerializeObject(value); } else { val = Serialize(value); } return(CacheDatabase.SetMove(sourcekey, destinationkey, val)); }
private static XmlNode GetEntityNode(Entity entity, XmlDocument result, SerializationStyle style) { switch (style) { case SerializationStyle.Basic: { XmlNode xEntity = result.CreateElement("Entity"); XmlAttribute xEntityName = result.CreateAttribute("name"); xEntityName.Value = entity.LogicalName; xEntity.Attributes.Append(xEntityName); XmlAttribute xEntityId = result.CreateAttribute("id"); xEntityId.Value = entity.Id.ToString(); xEntity.Attributes.Append(xEntityId); return xEntity; } case SerializationStyle.Explicit: { XmlNode xEntity = result.CreateElement(entity.LogicalName); XmlAttribute xEntityId = result.CreateAttribute("id"); xEntityId.Value = entity.Id.ToString(); xEntity.Attributes.Append(xEntityId); return xEntity; } default: return null; } }
private static XmlNode GetAttributeNode(XmlDocument result, KeyValuePair<string, object> attribute, SerializationStyle style) { switch (style) { case SerializationStyle.Basic: XmlNode xAttribute = result.CreateNode(XmlNodeType.Element, "Attribute", ""); XmlAttribute xName = result.CreateAttribute("name"); xName.Value = attribute.Key; xAttribute.Attributes.Append(xName); return xAttribute; case SerializationStyle.Explicit: return result.CreateNode(XmlNodeType.Element, attribute.Key, ""); default: return null; } }
public static XmlDocument Serialize(Entity entity, XmlNode parent, SerializationStyle style) { XmlDocument result; if (parent != null) { result = parent.OwnerDocument; } else { result = new XmlDocument(); parent = result.CreateElement("Entities"); result.AppendChild(parent); } XmlNode xEntity = GetEntityNode(entity, result, style); foreach (KeyValuePair<string, object> attribute in entity.Attributes) { if (attribute.Key == entity.LogicalName + "id") { // Don't include PK continue; } XmlNode xAttribute = GetAttributeNode(result, attribute, style); object value = attribute.Value; if (value is AliasedValue) { if (!string.IsNullOrEmpty(((AliasedValue)value).EntityLogicalName)) { XmlAttribute xAliasedEntity = result.CreateAttribute("entitylogicalname"); xAliasedEntity.Value = ((AliasedValue)value).EntityLogicalName; xAttribute.Attributes.Append(xAliasedEntity); } if (!string.IsNullOrEmpty(((AliasedValue)value).AttributeLogicalName)) { XmlAttribute xAliasedAttribute = result.CreateAttribute("attributelogicalname"); xAliasedAttribute.Value = ((AliasedValue)value).AttributeLogicalName; xAttribute.Attributes.Append(xAliasedAttribute); } value = ((AliasedValue)value).Value; } XmlAttribute xType = result.CreateAttribute("type"); xType.Value = LastClassName(value); xAttribute.Attributes.Append(xType); if (value is EntityReference) { XmlAttribute xRefEntity = result.CreateAttribute("entity"); xRefEntity.Value = ((EntityReference)value).LogicalName; xAttribute.Attributes.Append(xRefEntity); if (!string.IsNullOrEmpty(((EntityReference)value).Name)) { XmlAttribute xRefValue = result.CreateAttribute("value"); xRefValue.Value = ((EntityReference)value).Name; xAttribute.Attributes.Append(xRefValue); } } object basetypevalue = AttributeToBaseType(value); if (basetypevalue != null) { XmlText xValue = result.CreateTextNode(basetypevalue.ToString()); xAttribute.AppendChild(xValue); } xEntity.AppendChild(xAttribute); } parent.AppendChild(xEntity); return result; }
public static XmlDocument Serialize(Entity entity, XmlNode parent, SerializationStyle style) { XmlDocument result; if (parent != null) { result = parent.OwnerDocument; } else { result = new XmlDocument(); parent = result.CreateElement("Entities"); result.AppendChild(parent); } XmlNode xEntity = GetEntityNode(entity, result, style); foreach (KeyValuePair <string, object> attribute in entity.Attributes) { if (attribute.Key == entity.LogicalName + "id") { // Don't include PK continue; } XmlNode xAttribute = GetAttributeNode(result, attribute, style); object value = attribute.Value; if (value is AliasedValue) { if (!string.IsNullOrEmpty(((AliasedValue)value).EntityLogicalName)) { XmlAttribute xAliasedEntity = result.CreateAttribute("entitylogicalname"); xAliasedEntity.Value = ((AliasedValue)value).EntityLogicalName; xAttribute.Attributes.Append(xAliasedEntity); } if (!string.IsNullOrEmpty(((AliasedValue)value).AttributeLogicalName)) { XmlAttribute xAliasedAttribute = result.CreateAttribute("attributelogicalname"); xAliasedAttribute.Value = ((AliasedValue)value).AttributeLogicalName; xAttribute.Attributes.Append(xAliasedAttribute); } value = ((AliasedValue)value).Value; } XmlAttribute xType = result.CreateAttribute("type"); xType.Value = LastClassName(value); xAttribute.Attributes.Append(xType); if (value is EntityReference) { XmlAttribute xRefEntity = result.CreateAttribute("entity"); xRefEntity.Value = ((EntityReference)value).LogicalName; xAttribute.Attributes.Append(xRefEntity); if (!string.IsNullOrEmpty(((EntityReference)value).Name)) { XmlAttribute xRefValue = result.CreateAttribute("value"); xRefValue.Value = ((EntityReference)value).Name; xAttribute.Attributes.Append(xRefValue); } } object basetypevalue = AttributeToBaseType(value); if (basetypevalue != null) { XmlText xValue = result.CreateTextNode(basetypevalue.ToString()); xAttribute.AppendChild(xValue); } xEntity.AppendChild(xAttribute); } parent.AppendChild(xEntity); return(result); }
/// <summary> /// 异步根据索引的起始终止值获取有序递增排列集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="start"></param> /// <param name="end"></param> /// <param name="sstyle"></param> /// <returns></returns> public static Task <List <T> > SortSetRangeAsync <T>(string key, long start, long end, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { return SortSetRange <T>(key, start, end, sstyle); })); }
/// <summary> /// 异步设置集合移动 /// </summary> /// <param name="sourcekey">原始key</param> /// <param name="destinationkey">目标key</param> /// <param name="value"></param> /// <param name="sstyle"></param> public static Task <bool> SetMoveAsync <T>(string sourcekey, string destinationkey, T value, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { return SetMove(sourcekey, destinationkey, value, sstyle); })); }
/// <summary> /// 异步覆盖到有序集合:value存在时覆盖,value不存在时添加 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="score"></param> /// <param name="sstyle"></param> public static Task <bool> SortSetCoverAsync <T>(string key, T value, long score, SerializationStyle sstyle = SerializationStyle.Binary) { return(Task.Factory.StartNew(() => { RedisValue val; if (sstyle == SerializationStyle.Json) { val = JsonConvert.SerializeObject(value); } else { val = Serialize(value); } //删除已存在的数据 CacheDatabase.SortedSetRemove(key, val); //向有序集合中新增 return CacheDatabase.SortedSetAdd(key, val, score); })); }
private static XmlNode GetAttributeNode(XmlDocument result, KeyValuePair <string, object> attribute, SerializationStyle style) { switch (style) { case SerializationStyle.Basic: XmlNode xAttribute = result.CreateNode(XmlNodeType.Element, "Attribute", ""); XmlAttribute xName = result.CreateAttribute("name"); xName.Value = attribute.Key; xAttribute.Attributes.Append(xName); return(xAttribute); case SerializationStyle.Explicit: return(result.CreateNode(XmlNodeType.Element, attribute.Key, "")); default: return(null); } }
/// <summary> /// 异步覆盖到有序集合:value存在时覆盖,value不存在时添加 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="sstyle"></param> public static Task <bool> SortSetCoverAsync <T>(string key, T value, SerializationStyle sstyle = SerializationStyle.Binary) { long score = DateTime.Now.Ticks - new DateTime(2016, 10, 1).Ticks; return(SortSetCoverAsync(key, value, score, sstyle)); }