Ejemplo n.º 1
0
        public async Task <TKey> SaveAsync(EasyStorableObject <TKey> obj)
        {
            if (obj == null)
            {
                return(default(TKey));
            }

            var keyType = typeof(TKey);

            if (keyType == typeof(Guid))
            {
                var objId = (Guid)Convert.ChangeType(obj.Id, typeof(Guid));
                if (objId == null || objId == Guid.Empty)
                {
                    var id = Guid.NewGuid();
                    obj.Id = (TKey)Convert.ChangeType(id, keyType);
                }
            }

            if (keyType == typeof(long))
            {
                var objId = (long)Convert.ChangeType(obj.Id, typeof(long));
                if (objId <= 0)
                {
                    var id = EasyAutoIncreament.NextId(nameof(TEntity), _StoragePath);
                    obj.Id = (TKey)Convert.ChangeType(id, keyType);
                }
            }

            await File.WriteAllTextAsync(Path.Combine(_StoragePath, obj.Id + ".json"), JsonConvert.SerializeObject(obj));

            return(obj.Id);
        }
Ejemplo n.º 2
0
        public async Task <Guid> SaveAsync(EasyStorableObject <Guid> obj)
        {
            if (obj == null)
            {
                return(Guid.Empty);
            }

            if (obj.Id.Equals(Guid.Empty))
            {
                obj.Id = Guid.NewGuid();
            }

            var idstring = obj.Id.ToString();
            var _lock    = _locks.GetOrAdd(idstring, _ => new SemaphoreSlim(1, 1));

            try
            {
                await _lock.WaitAsync();

                _cache.Set(idstring, obj,
                           new MemoryCacheEntryOptions()
                           .SetAbsoluteExpiration(TimeSpan.FromMinutes(15))
                           .RegisterPostEvictionCallback(
                               (key, value, reason, substate) => _lock.Release()
                               ));
            }
            catch { _lock.Release(); }
            finally { _lock.Release(); }
            return(obj.Id);
        }
Ejemplo n.º 3
0
        public virtual async Task <string> SaveAsync(EasyStorableObject <string> obj)
        {
            if (obj == null)
            {
                return(string.Empty);
            }

            if (obj.Id?.Trim() == "")
            {
                obj.Id = null;
            }

            using (var ctx = RavenDbDocumentStoreHolder.Store.OpenAsyncSession())
            {
                await ctx.StoreAsync(obj);

                await ctx.SaveChangesAsync();
            }
            return(obj.Id);
        }
Ejemplo n.º 4
0
 public virtual string Save(EasyStorableObject <string> obj)
 {
     return(SaveAsync(obj).GetAwaiter().GetResult());
 }
Ejemplo n.º 5
0
 public TKey Save(EasyStorableObject <TKey> obj)
 {
     return(SaveAsync(obj).GetAwaiter().GetResult());
 }
Ejemplo n.º 6
0
 public Guid Save(EasyStorableObject <Guid> obj)
 {
     return(SaveAsync(obj).GetAwaiter().GetResult());
 }