public ICacheable Get(Type type, string id) { if (string.IsNullOrEmpty(id)) { return(null); } string fullName = type.FullName; if (!IsNeedCached(fullName)) { return(null); } SingleTypeCacheConfig config = SingleTypeCacheConfig.Get(fullName); Dictionary <string, MyCacheObject> caches = GetCaches(fullName); config.LockObj.EnterReadLock(); try { id = id.ToLower(); if (!caches.ContainsKey(id)) { return(null); } MyCacheObject myCacheObject = caches[id]; if (!config.IsFix && myCacheObject.OverdueTime < DateTime.Now) { return(null); } myCacheObject.LastGetTime = DateTime.Now; try { config.Getted(myCacheObject.Obj); } catch (Exception ex) { LogProxy.Error(ex, false); } return(myCacheObject.Obj); } catch (Exception ex) { LogProxy.Error(ex, false); } finally { config.LockObj.ExitReadLock(); } return(null); }
public void Set(ICacheable obj) { if (obj == null) { return; } string fullName = obj.GetType().FullName; if (!IsNeedCached(fullName)) { return; } SingleTypeCacheConfig config = SingleTypeCacheConfig.Get(fullName); Dictionary <string, MyCacheObject> caches = GetCaches(fullName); if (!config.LockObj.TryEnterWriteLock(WriteLockTimeout)) { return; } try { string id = obj.Id.ToLower(); MyCacheObject myCacheObject; if (caches.ContainsKey(id)) { myCacheObject = caches[id]; if (myCacheObject.Obj.Version >= obj.Version) { return; } myCacheObject.Obj = obj; myCacheObject.OverdueTime = DateTime.Now.AddSeconds(config.Second); } else { myCacheObject = new MyCacheObject(obj, DateTime.Now.AddSeconds(config.Second)); caches.Add(id, myCacheObject); } try { config.Setted(obj); } catch (Exception ex) { LogProxy.Error(ex, false); } } catch (Exception ex) { LogProxy.Error(ex, false); } finally { config.LockObj.ExitWriteLock(); } }