protected virtual object ResolveOutput(MemcachedCacheItem item, Type targetType) { if (item.Flags == FLAG_JSON) { if (targetType == TYPE_STRING) { return(Encoding.UTF8.GetString(item.ValueBytes)); } return(JsonUtil.Deserialize(Encoding.UTF8.GetString(item.ValueBytes), targetType)); } else if (item.Flags == FLAG_STRING) { return(TypeConversionUtil.ConvertValueIfNecessary(targetType, Encoding.UTF8.GetString(item.ValueBytes))); } else { if (targetType == TYPE_STRING) { return(item.ValueBytes.ToBase64()); } return(TypeConversionUtil.ConvertValueIfNecessary(targetType, item.ValueBytes)); } }
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 MemcachedCacheItem() { Key = ResolveKey(key) }; ResolveInput(item, value); if (item.ValueBytes == null) { throw new ArgumentException($"Argument '{value}' cannot be empty."); } using (var client = GetMemcachedClient(shardObject, true)) { return(client.Replace(item.Key, item.ValueBytes, item.Flags, ttl)); } }
public object Get(object shardObject, string key, Type objectType) { AssertUtil.ArgumentNotEmpty(key, nameof(key)); MemcachedCacheItem item = null; using (var client = GetMemcachedClient(shardObject, false)) { item = client.Get(ResolveKey(key)); } if (item == null) { throw new KeyNotFoundException(); } return(ResolveOutput(item, objectType)); }
protected virtual void ResolveInput(MemcachedCacheItem item, object value) { if (value is string || value.GetType().IsPrimitive) { item.Flags = FLAG_STRING; item.ValueBytes = Encoding.UTF8.GetBytes(value.ToString()); } else if (value is DateTime || value is DateTime?) { item.Flags = FLAG_STRING; item.ValueBytes = Encoding.UTF8.GetBytes(((DateTime)value).ToString("yyyy-MM-ddTHH:mm:ss.fffzzz")); } else if (value is ICollection <byte> ) { item.Flags = FLAG_BINARY; item.ValueBytes = (value as ICollection <byte>).ToArray(); } else { item.Flags = FLAG_JSON; item.ValueBytes = Encoding.UTF8.GetBytes(JsonUtil.Serialize(value)); } }
private MemcachedCacheItem[] ExecGetCommand(string cmd, string[] keys) { ThrowIfObjectDisposed(); ThrowIfNotConnected(); try { m_pClient.WriteLine($"{cmd} {string.Join(" ", keys)}"); var list = new List <MemcachedCacheItem>(); while (true) { string line = m_pClient.ReadLine(); if (line == string.Empty) { line = m_pClient.ReadLine(); } if (line.Equals(END, StringComparison.InvariantCultureIgnoreCase)) { break; } if (line.Equals(ERROR, StringComparison.InvariantCultureIgnoreCase)) // not happended yet. { throw new Exception($"Error."); } if (line.StartsWith(CLIENT_ERROR, StringComparison.InvariantCultureIgnoreCase)) { throw new Exception($"Client error: {line.Split(' ', 2)[1]}"); } if (line.StartsWith(SERVER_ERROR, StringComparison.InvariantCultureIgnoreCase)) { throw new Exception($"Server error: {line.Split(' ', 2)[1]}"); } if (!line.StartsWith(VALUE, StringComparison.InvariantCultureIgnoreCase)) { throw new NotSupportedException($"Unsupported response: {line}"); } var items = line.Split(' ', 5, StringSplitOptions.RemoveEmptyEntries); var kvItem = new MemcachedCacheItem(); kvItem.Key = items[1]; kvItem.Flags = int.Parse(items[2]); kvItem.Length = int.Parse(items[3]); kvItem.Revision = items.Length > 4 ? int.Parse(items[4]) : 0; using (var toStream = new MemoryStream()) { FixedCountReader.Read(m_pClient.TcpStream, toStream, kvItem.Length); kvItem.ValueBytes = toStream.ToArray(); } m_pClient.ReadLine(); // remove end crlf. list.Add(kvItem); } return(list.ToArray()); } catch (SocketException ex) { m_pClient.Dispose(); throw ex; } catch (IOException ex) { m_pClient.Dispose(); throw ex; } }