예제 #1
0
        public bool Replace(object shardObject, string key, object value, int ttl)
        {
            ThrowIfObjectDisposed();

            AssertUtil.ArgumentNotEmpty(key, nameof(key));
            AssertUtil.ArgumentNotNull(value, nameof(value));

            if (ttl <= 0)
            {
                throw new ArgumentException($"Expire time time must be > 0.");
            }


            var item = GetItem(key);

            if (item == null)
            {
                return(false);
            }

            var temp = new MemoryCacheItem();

            ResolveInput(temp, value);
            if (temp.StringValue == null && temp.BinaryValue == null)
            {
                throw new ArgumentException($"Argument '{nameof(value)}' cannot be empty.");
            }

            item.StringValue = temp.StringValue;
            item.BinaryValue = temp.BinaryValue;
            item.ExpireTime  = DateTime.Now.AddSeconds(ttl);

            return(true);
        }
예제 #2
0
        public bool Remove(object shardObject, string key)
        {
            ThrowIfObjectDisposed();

            AssertUtil.ArgumentNotEmpty(key, nameof(key));


            if (ItemsByKey.TryGetValue(key, out MemoryCacheItem item))
            {
                ItemsByKey.Remove(key);

                if (item == First)
                {
                    First          = item.Next;
                    First.Previous = null;
                }
                else
                {
                    if (item.Previous != null)
                    {
                        item.Previous.Next = item.Next;
                    }

                    item.Next.Previous = item.Previous;
                }

                return(!item.IsExpired);
            }

            return(false);
        }
예제 #3
0
        protected virtual object ResolveOutput(MemoryCacheItem item, Type targetType)
        {
            if (item.ValueType == CacheValueType.Json)
            {
                if (targetType == TYPE_STRING)
                {
                    return(item.StringValue);
                }

                return(JsonUtil.Deserialize(item.StringValue, targetType));
            }
            else if (item.ValueType == CacheValueType.Plain)
            {
                return(TypeConversionUtil.ConvertValueIfNecessary(targetType, item.StringValue));
            }
            else if (item.ValueType == CacheValueType.Binary)
            {
                if (targetType == TYPE_STRING)
                {
                    return(item.BinaryValue.ToBase64());
                }

                return(TypeConversionUtil.ConvertValueIfNecessary(targetType, item.BinaryValue));
            }
            else
            {
                throw new Exception();
            }
        }
예제 #4
0
        public MemoryCache(int capacity = 100000)
        {
            if (capacity < 1000)
            {
                throw new ArgumentException($"Argument '{nameof(capacity)}' value must be >= 1000.");
            }

            MaxCapacity = capacity;
            ItemsByKey  = new Dictionary <string, MemoryCacheItem>(capacity);

            First = Last = new MemoryCacheItem();

            ClearTimer          = new TimerEx(60 * 1000);
            ClearTimer.Elapsed += Clear_Elapsed;
            ClearTimer.Start();
        }
예제 #5
0
        public bool Add(object shardObject, string key, object value, int ttl)
        {
            ThrowIfObjectDisposed();

            AssertUtil.ArgumentNotEmpty(key, nameof(key));
            AssertUtil.ArgumentNotNull(value, nameof(value));

            if (ttl <= 0)
            {
                throw new ArgumentException($"Expire time must be > 0.");
            }


            if (GetItem(key) != null)
            {
                return(false);
            }

            var item = new MemoryCacheItem()
            {
                Key        = key,
                ExpireTime = DateTime.Now.AddSeconds(ttl)
            };

            ResolveInput(item, value);
            if (item.StringValue == null && item.BinaryValue == null)
            {
                throw new ArgumentException($"Argument '{nameof(value)}' cannot be empty.");
            }

            if (ItemsByKey.Count == MaxCapacity)
            {
                Clear();
            }

            ItemsByKey[key] = item;

            item.Next      = First;
            First.Previous = item;
            First          = item;

            return(true);
        }
예제 #6
0
        private void MoveToFirst(MemoryCacheItem item)
        {
            if (First == item)
            {
                return;
            }

            if (item.Previous != null)
            {
                item.Previous.Next = item.Next;
            }

            item.Next.Previous = item.Previous;

            item.Next     = First;
            item.Previous = null;

            First = item;
        }
예제 #7
0
 protected virtual void ResolveInput(MemoryCacheItem item, object value)
 {
     if (value is string || value.GetType().IsPrimitive)
     {
         item.ValueType   = CacheValueType.Plain;
         item.StringValue = value.ToString();
     }
     else if (value is DateTime || value is DateTime?)
     {
         item.ValueType   = CacheValueType.Plain;
         item.StringValue = ((DateTime)value).ToString("yyyy-MM-ddTHH:mm:ss.fffzzz");
     }
     else if (value is ICollection <byte> )
     {
         item.ValueType   = CacheValueType.Binary;
         item.BinaryValue = new List <byte>(value as ICollection <byte>).ToArray();
     }
     else
     {
         item.ValueType   = CacheValueType.Json;
         item.StringValue = JsonUtil.Serialize(value);
     }
 }