protected virtual object ResolveOutput(RedisKvCacheItem item, Type targetType) { if (targetType.IsArray) { foreach (var t in targetType.GetInterfaces()) { if (t == typeof(ICollection <byte>)) { return(TypeConversionUtil.ConvertValueIfNecessary(targetType, item.ValueBytes.FromBase64())); } } return(JsonUtil.Deserialize(item.ValueBytes.ToUtf8String(), targetType)); } if (targetType == TYPE_STRING || targetType.IsPrimitive) { return(TypeConversionUtil.ConvertValueIfNecessary(targetType, Encoding.UTF8.GetString(item.ValueBytes))); } if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Nullable <>) && Nullable.GetUnderlyingType(targetType).IsPrimitive) { return(TypeConversionUtil.ConvertValueIfNecessary(targetType, Encoding.UTF8.GetString(item.ValueBytes))); } if (targetType == typeof(DateTime) || targetType == typeof(DateTime?)) { return(TypeConversionUtil.ConvertValueIfNecessary(targetType, Encoding.UTF8.GetString(item.ValueBytes))); } return(JsonUtil.Deserialize(Encoding.UTF8.GetString(item.ValueBytes), targetType)); }
public bool Replace(object shardObject, string key, object value, int ttl) { AssertUtil.ArgumentNotEmpty(key, nameof(key)); AssertUtil.ArgumentNotNull(value, nameof(value)); if (ttl <= 0) { throw new ArgumentException($"Expire time must be > 0."); } var item = new RedisKvCacheItem() { Key = ResolveKey(key) }; ResolveInput(item, value); if (item.StringValue == null) { throw new ArgumentException($"Argument '{value}' cannot be empty."); } using (var client = GetRedisClient(shardObject, true)) { return(client.Replace(item.Key, item.StringValue, ttl)); } }
public RedisKvCacheItem Get(string key) { ThrowIfObjectDisposed(); ThrowIfNotConnected(); try { SendCommand("GET", key); var response = ReadResponse(); if (response.Error != null) { throw new Exception(response.Error); } var kvItem = new RedisKvCacheItem(); kvItem.Key = key; kvItem.ValueBytes = (byte[])response.Value; return(kvItem); } catch (SocketException ex) { m_pClient.Dispose(); throw ex; } catch (IOException ex) { m_pClient.Dispose(); throw ex; } }
public object Get(object shardObject, string key, Type objectType) { AssertUtil.ArgumentNotEmpty(key, nameof(key)); RedisKvCacheItem item = null; using (var client = GetRedisClient(shardObject, false)) { item = client.Get(ResolveKey(key)); } if (item == null) { throw new KeyNotFoundException(); } return(ResolveOutput(item, objectType)); }
protected virtual void ResolveInput(RedisKvCacheItem item, object value) { if (value is string || value.GetType().IsPrimitive) { item.StringValue = value.ToString(); } else if (value is DateTime || value is DateTime?) { item.StringValue = ((DateTime)value).ToString("yyyy-MM-ddTHH:mm:ss.fffzzz"); } else if (value is ICollection <byte> ) { item.StringValue = (value as ICollection <byte>).ToArray().ToBase64(); } else { item.StringValue = JsonUtil.Serialize(value); } }
public RedisKvCacheItem[] Get(string[] keys) { ThrowIfObjectDisposed(); ThrowIfNotConnected(); try { var args = new List <string>(keys.Length + 1); args.Add("MGET"); args.AddRange(keys); SendCommand(args.ToArray()); var response = ReadResponse(); if (response.Error != null) { throw new Exception(response.Error); } var values = (object[])response.Value; var list = new List <RedisKvCacheItem>(); for (int i = 0; i < keys.Length; i++) { var kvItem = new RedisKvCacheItem(); kvItem.Key = keys[i]; kvItem.ValueBytes = (byte[])values[i]; list.Add(kvItem); } return(list.ToArray()); } catch (SocketException ex) { m_pClient.Dispose(); throw ex; } catch (IOException ex) { m_pClient.Dispose(); throw ex; } }