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); } }
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)); } } }