Ejemplo n.º 1
0
 public async Task <bool> Delete(Guid id)
 {
     using (var l = await _lock.LockAsync())
     {
         var f = _getFile(id);
         return(await _localStorageFileRepo.Delete(f));
     }
 }
Ejemplo n.º 2
0
        public async Task <T> GetEntity <T>(string key) where T : class, new()
        {
            var fullName = _getFullKey <T>(key);

            var f = await _getMemory <T>(key);

            if (f == null)
            {
                var locker = XNamedLock.Get(key + "3");
                using (var locked = await locker.LockAsync())
                {
                    f = await _getMemory <T>(key);

                    if (f == null)
                    {
                        Debug.WriteLine($"*** Entity cache miss: {fullName}");

                        f = await _storageFileRepo.Get <XCacheItem <T> >(fullName);

                        if (f != null && f.Item != null)
                        {
                            Debug.WriteLine($"      Found: {fullName}");
                            _updateItem(f.Item, f);

                            if (!_validateAge(f))
                            {
                                //delete it from storage
                                await _storageFileRepo.Delete(fullName);

                                return(null);
                            }

                            TimeSpan?toLiveFromNow = f.MaxAge != null
                                ? f.DateStamp.Add(f.MaxAge.Value).Subtract(DateTime.UtcNow)
                                : TimeSpan.FromDays(30);

                            if (toLiveFromNow.Value.Milliseconds < 0)
                            {
                                //this item is in the past!
                                //Top it up with another day.
                                toLiveFromNow = TimeSpan.FromDays(1);
                            }

                            var cacheEntity = await _setMemory(key, f.Item, toLiveFromNow);

                            _updateItem(cacheEntity.Item, cacheEntity);
                        }
                    }

                    if (f == null)
                    {
                        Debug.WriteLine($"      Not found: {fullName}");
                        return(null);
                    }
                }
            }

            //else
            //{
            //    Debug.WriteLine($"Entity cache hit: {fullName}");
            //}


            _updateItemCacheSource(f.Item, true);

            return(_validateAge(f) ? f.Item : null);
        }