예제 #1
0
 public bool Exist(string key)
 {
     if (ValidateService.IsNullOrEmpty(key))
     {
         throw new ArgumentNullException("key", string.Format("key is null or empty in the {0} cahced.", _name));
     }
     lock (_locker)
     {
         if (_ht.ContainsKey(key))
         {
             WeakReference objWeak = (WeakReference)_ht[key];
             if (null == objWeak || !objWeak.IsAlive || null == objWeak.Target)
             {
                 _ht.Remove(key);
                 return(false);
             }
             IAlbianCachedObject obj = (IAlbianCachedObject)objWeak.Target;
             if (((int)DateTime.Now.Subtract(obj.CreateTime).TotalSeconds) <= obj.Timespan)
             {
                 if (_logger != null)
                 {
                     _logger.WarnFormat("key:{0} in {1} cached is expired.", key, _name);
                 }
                 return(true);
             }
             return(false);
         }
         return(false);
     }
 }
예제 #2
0
 public void Set(string key, object value)
 {
     if (ValidateService.IsNullOrEmpty(key))
     {
         throw new ArgumentNullException("key", string.Format("key is null or empty in the {0} chunk.", _name));
     }
     lock (_locker)
     {
         if (_ht.ContainsKey(key))
         {
             IAlbianChunkObject obj = new AlbianChunkObject
             {
                 Key        = key,
                 Value      = value,
                 CreateTime = DateTime.Now
             };
             _ht[key] = obj;
         }
         else
         {
             IAlbianChunkObject obj = new AlbianChunkObject
             {
                 Key        = key,
                 Value      = value,
                 CreateTime = DateTime.Now
             };
             _ht.Add(key, obj);
         }
     }
 }
예제 #3
0
        public void Update(string key, object value, int seconds)
        {
            if (ValidateService.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key", string.Format("key is null or empty in the {0} cahced.", _name));
            }
            if (0 >= seconds)
            {
                throw new ArgumentNullException("seconds",
                                                string.Format("seconds is less or eq 0 in the {0} cahced.", _name));
            }

            lock (_locker)
            {
                if (!_ht.ContainsKey(key))
                {
                    throw new AlbianCachedException(string.Format("the {0} cached is not exist in the {1} cached.", key,
                                                                  _name));
                }
                IAlbianCachedObject obj = new AlbianCachedObject
                {
                    Timespan   = seconds,
                    Key        = key,
                    Value      = value,
                    CreateTime = DateTime.Now
                };
                _ht[key] = WeakReferenceObject.CreateWeakReferenceObject(obj);
            }
        }
예제 #4
0
        public void Insert(string key, object value, int seconds)
        {
            if (ValidateService.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key", string.Format("key is null or empty in the {0} cahced.", _name));
            }
            if (0 >= seconds)
            {
                throw new ArgumentNullException("seconds", string.Format("seconds is less or eq 0 in the {0} cahced.", _name));
            }

            lock (_locker)
            {
                if (_ht.ContainsKey(key))
                {
                    WeakReference objWeak = (WeakReference)_ht[key];
                    if (null == objWeak || !objWeak.IsAlive || null == objWeak.Target)
                    {
                        IAlbianCachedObject obj = new AlbianCachedObject
                        {
                            Timespan   = seconds,
                            Key        = key,
                            Value      = value,
                            CreateTime = DateTime.Now
                        };
                        _ht[key] = WeakReferenceObject.CreateWeakReferenceObject(obj);
                    }
                    else
                    {
                        IAlbianCachedObject objOld = (IAlbianCachedObject)objWeak.Target;
                        if (((int)DateTime.Now.Subtract(objOld.CreateTime).TotalSeconds) <= objOld.Timespan)
                        {
                            throw new AlbianCachedException(string.Format("the {0} cached is exist in the {1} cached.", key,
                                                                          _name));
                        }
                        IAlbianCachedObject obj = new AlbianCachedObject
                        {
                            Timespan   = seconds,
                            Key        = key,
                            Value      = value,
                            CreateTime = DateTime.Now
                        };
                        _ht[key] = WeakReferenceObject.CreateWeakReferenceObject(obj);
                    }
                }
                else
                {
                    IAlbianCachedObject obj = new AlbianCachedObject
                    {
                        Timespan   = seconds,
                        Key        = key,
                        Value      = value,
                        CreateTime = DateTime.Now
                    };
                    _ht.Add(key, WeakReferenceObject.CreateWeakReferenceObject(obj));
                }
            }
        }
예제 #5
0
 public bool Exist(string key)
 {
     if (ValidateService.IsNullOrEmpty(key))
     {
         throw new ArgumentNullException("key", string.Format("key is null or empty in the {0} chunk.", _name));
     }
     lock (_locker)
     {
         return(_ht.ContainsKey(key));
     }
 }
예제 #6
0
        public void Remove(string key)
        {
            if (ValidateService.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key", string.Format("key is null or empty in the {0} cahced.", _name));
            }

            lock (_locker)
            {
                if (_ht.ContainsKey(key))
                {
                    _ht.Remove(key);
                }
            }
        }
예제 #7
0
 public object Get(string key)
 {
     if (ValidateService.IsNullOrEmpty(key))
     {
         throw new ArgumentNullException("key", string.Format("key is null or empty in the {0} chunk.", _name));
     }
     lock (_locker)
     {
         if (_ht.ContainsKey(key))
         {
             IAlbianChunkObject obj = (IAlbianChunkObject)_ht[key];
             return(obj.Value);
         }
         return(null);
     }
 }
예제 #8
0
        protected override IList <ICacheGroup> ParserLocalGroups(XmlNodeList nodes)
        {
            if (ValidateService.IsNullOrEmpty(nodes))
            {
            }
            IList <ICacheGroup> groups = new List <ICacheGroup>();

            foreach (XmlNode node in nodes)
            {
                ICacheGroup group = ParserLocalGroup(node);
                if (null == group)
                {
                    continue;
                }
                groups.Add(group);
            }
            return(groups);
        }
예제 #9
0
 public void Update(string key, object value)
 {
     if (ValidateService.IsNullOrEmpty(key))
     {
         throw new ArgumentNullException("key", string.Format("key is null or empty in the {0} chunk.", _name));
     }
     lock (_locker)
     {
         if (!_ht.ContainsKey(key))
         {
             throw new AlbianChunkException(string.Format("the {0} cached is not exist in the {1} chunk.", key,
                                                          _name));
         }
         IAlbianChunkObject obj = new AlbianChunkObject
         {
             Key        = key,
             Value      = value,
             CreateTime = DateTime.Now
         };
         _ht[key] = obj;
     }
 }
예제 #10
0
        public object Get(string key)
        {
            if (ValidateService.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key", string.Format("key is null or empty in the {0} cahced.", _name));
            }
            lock (_locker)
            {
                if (_ht.ContainsKey(key))
                {
                    WeakReference objWeak = (WeakReference)_ht[key];
                    if (null == objWeak || !objWeak.IsAlive || null == objWeak.Target)
                    {
                        _ht.Remove(key);
                        return(null);
                    }
                    IAlbianCachedObject obj = (IAlbianCachedObject)objWeak.Target;
                    return(((int)DateTime.Now.Subtract(obj.CreateTime).TotalSeconds) <= obj.Timespan ? obj.Value : null);

                    ;
                }
                return(null);
            }
        }