コード例 #1
0
ファイル: MemoryCache.cs プロジェクト: MH1/Kraken.Caching
        private void Save <TResult>(CacheParams par, TResult data)
        {
            bool exists = true;

            if (!localCache.ContainsKey(par.Key))
            {
                lock (cacheLock)
                    if (!localCache.ContainsKey(par.Key))
                    {
                        exists = false;
                        CachedObject <TResult> item = new CachedObject <TResult>
                        {
                            Data           = data,
                            Tags           = par.Tags,
                            ExpirationDate = DateTime.UtcNow.AddMinutes(par.Duration),
                        };
                        localCache.Add(par.Key, item);
                    }
            }

            if (exists)
            {
                CachedObject <TResult> item = localCache[par.Key] as CachedObject <TResult>;
                lock (item.Lock)
                {
                    if (item.Data is IDisposable disposable)
                    {
                        disposable.Dispose();
                    }
                    item.Data           = data;
                    item.Tags           = par.Tags;
                    item.ExpirationDate = DateTime.UtcNow.AddMinutes(par.Duration);
                }
            }
        }
コード例 #2
0
ファイル: MemoryCache.cs プロジェクト: MH1/Kraken.Caching
 public void Set <TService>(
     CachedServiceBase <TService> cached,
     CacheParams par,
     Action <CachedServiceBase <TService> > setData)
     where TService : class
 {
     setData(cached);
     Invalidate(par);
 }
コード例 #3
0
ファイル: MemoryCache.cs プロジェクト: MH1/Kraken.Caching
        public async Task SetAsync <TService>(
            CachedServiceBase <TService> cached,
            CacheParams par,
            Func <CachedServiceBase <TService>, Task> setData)
            where TService : class
        {
            await setData(cached);

            Invalidate(par);
        }
コード例 #4
0
ファイル: MemoryCache.cs プロジェクト: MH1/Kraken.Caching
 public void Set <TService, TResult>(
     CachedServiceBase <TService> cached,
     CacheParams par,
     TResult value,
     Action <CachedServiceBase <TService>, TResult> setData)
     where TService : class
 {
     setData(cached, value);
     Save(par, value);
 }
コード例 #5
0
ファイル: MemoryCache.cs プロジェクト: MH1/Kraken.Caching
        public async Task SetAsync <TService, TResult>(
            CachedServiceBase <TService> cached,
            CacheParams par,
            TResult value,
            Func <CachedServiceBase <TService>, TResult, Task> setData)
            where TService : class
        {
            await setData(cached, value);

            Save(par, value);
        }
コード例 #6
0
ファイル: MemoryCache.cs プロジェクト: MH1/Kraken.Caching
 private void Invalidate(CacheParams par)
 {
     if (localCache.ContainsKey(par.Key))
     {
         lock (cacheLock)
             if (localCache.ContainsKey(par.Key))
             {
                 CachedObject item = localCache[par.Key] as CachedObject;
                 if (item is IDisposable disposable)
                 {
                     disposable.Dispose();
                 }
                 localCache.Remove(par.Key);
             }
     }
 }
コード例 #7
0
 public void Set <TService>(
     CachedServiceBase <TService> cached,
     CacheParams par,
     Action <CachedServiceBase <TService> > setData)
     where TService : class
 => memoryCache.Set(cached, par, service =>
 {
     setData(service);
     if (this is IRemoteCache remote0)
     {
         remote0.Invalidate(par);
     }
     else if (this is IRemoteCacheAsync remoteAsync0)
     {
         Task task = remoteAsync0.InvalidateAsync(par);
         task.Wait();
     }
 });
コード例 #8
0
 public void Set <TService, TResult>(
     CachedServiceBase <TService> cached,
     CacheParams par,
     TResult value,
     Action <CachedServiceBase <TService>, TResult> setData)
     where TService : class
 => memoryCache.Set(cached, par, value, (service, value) =>
 {
     setData(service, value);
     if (this is IRemoteCache remote0)
     {
         remote0.Save(par, value);
     }
     else if (this is IRemoteCacheAsync remoteAsync0)
     {
         Task task = remoteAsync0.SaveAsync(par, value);
         task.Wait();
     }
 });
コード例 #9
0
        public TResult Get <TService, TResult>(
            CachedServiceBase <TService> cached,
            CacheParams par,
            Func <CachedServiceBase <TService>, TResult> getData)
            where TService : class
        => memoryCache.Get(cached, par, service =>
        {
            TResult data = default;
            if (this is IRemoteCache remote)
            {
                if (remote.Contains(par.Key))
                {
                    data = remote.Load <TResult>(par);
                    return(data);
                }
            }
            else if (this is IRemoteCacheAsync remoteAsync)
            {
                if (remoteAsync.Contains(par.Key))
                {
                    Task <TResult> task = remoteAsync.LoadAsync <TResult>(par);
                    task.Wait();
                    data = task.Result;
                    return(data);
                }
            }
            else
            {
                throw new InvalidOperationException($"Manager {GetType().FullName} has is not implemented.");
            }

            data = getData(cached);
            if (this is IRemoteCache remote0)
            {
                remote0.Save(par, data);
            }
            else if (this is IRemoteCacheAsync remoteAsync0)
            {
                Task task = remoteAsync0.SaveAsync(par, data);
                task.Wait();
            }
            return(data);
        });
コード例 #10
0
ファイル: MemoryCache.cs プロジェクト: MH1/Kraken.Caching
        public async Task <TResult> GetAsync <TService, TResult>(
            CachedServiceBase <TService> cached,
            CacheParams par,
            Func <CachedServiceBase <TService>, Task <TResult> > getData)
            where TService : class
        {
            if (localCache.ContainsKey(par.Key))
            {
                lock (cacheLock)
                    if (localCache.ContainsKey(par.Key))
                    {
                        CachedObject item = localCache[par.Key] as CachedObject;
                        if (!item.IsExpired)
                        {
                            return((TResult)item.Data);
                        }
                    }
            }
            TResult data = await getData(cached);

            Save(par, data);
            return(data);
        }
コード例 #11
0
 public async Task <TResult> GetAsync <TService, TResult>(
     CachedServiceBase <TService> cached,
     CacheParams par,
     Func <CachedServiceBase <TService>, Task <TResult> > getData)
     where TService : class
 => await memoryCache.GetAsync(cached, par, async service =>