Пример #1
0
        public override void Set(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
        {
            PrimaryCache.Set(key, value, absoluteExpiration, regionName);

            //queue up writing to secondary cache on another thread
            ThreadPool.QueueUserWorkItem(_ => SecondaryCache.Set(key, PrimaryCache.Get(key), absoluteExpiration, regionName));
        }
Пример #2
0
        public override void Set(CacheItem item, CacheItemPolicy policy)
        {
            PrimaryCache.Set(item, policy);

            //queue up writing to secondary cache on another thread
            ThreadPool.QueueUserWorkItem(_ => SecondaryCache.Set(PrimaryCache.GetCacheItem(item.Key, item.RegionName), policy));
        }
Пример #3
0
        public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
        {
            PrimaryCache.Set(key, value, policy, regionName);

            //queue up writing to secondary cache on another thread
            ThreadPool.QueueUserWorkItem(_ => SecondaryCache.Set(key, PrimaryCache.Get(key), policy, regionName));
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="connectionInfo"></param>
        /// <param name="keys"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <IEnumerable <TEntity> > QueryByKeyAsync <TEntity, TKey>(IConnectionInfo connectionInfo, IEnumerable <TKey> keys, CancellationToken token = default) where TEntity : class, IEntity
        {
            await using var connection = connectionInfo.GetConnection <T>();
            var queryField = new QueryField(PrimaryCache.Get <TEntity>().AsField(), keys);

            return(await connection.QueryAsync <TEntity>(queryField, cancellationToken : token).ConfigureAwait(false));
        }
Пример #5
0
        public void TestPrimaryCacheForTypeOfDerivedClassWithoutPrimary()
        {
            // Act
            var primary = PrimaryCache.Get(typeof(DerivedClass));

            // Assert
            Assert.IsNull(primary);
        }
Пример #6
0
        public void TestPrimaryCacheForDerivedClassWithoutPrimary()
        {
            // Act
            var primary = PrimaryCache.Get <DerivedClass>();

            // Assert
            Assert.IsNull(primary);
        }
Пример #7
0
        public void TestPrimaryCacheForDerivedClass()
        {
            // Act
            var primary = PrimaryCache.Get <DerivedClassWithPrimaryAtBase>();

            // Assert
            Assert.IsNotNull(primary);
        }
Пример #8
0
        public void TestPrimaryCacheForBaseClass()
        {
            // Act
            var primary = PrimaryCache.Get <BaseClassWithPrimary>();

            // Assert
            Assert.IsNotNull(primary);
        }
Пример #9
0
        public void TestPrimaryClass()
        {
            // Act
            var primary = PrimaryCache.Get <PrimaryClass>();

            // Assert
            Assert.IsNotNull(primary);
        }
Пример #10
0
        /// <summary>
        /// Query count based on the entity key.
        /// </summary>
        /// <typeparam name="TEntity">Entity type</typeparam>
        /// <typeparam name="TKey">Key type</typeparam>
        /// <param name="connectionInfo">Connection info</param>
        /// <param name="key">entity key</param>
        /// <param name="token">Cancellation token</param>
        /// <returns>Query result</returns>
        public async Task <int> QueryCountAsync <TEntity, TKey>(IConnectionInfo connectionInfo, TKey key, CancellationToken token = default) where TEntity : class, IEntity
        {
            var keyColumnName = PrimaryCache.Get <TEntity>().GetMappedName();
            var tableName     = ClassMappedNameCache.Get <TEntity>();
            var result        = await ExecuteAsync(connectionInfo, $@"SELECT TOP 1 {keyColumnName} FROM {tableName} WHERE {keyColumnName} = {CheckSqlKey(key)}", null, token).ConfigureAwait(false);

            return(result);
        }
Пример #11
0
        public override object Remove(string key, string regionName = null)
        {
            var item = PrimaryCache.Remove(key, regionName);

            SecondaryCache.Remove(key, regionName);

            return(item);
        }
Пример #12
0
        public void TestPrimaryClassWithUniformPrimaryFromTheMapping()
        {
            // Act
            var primary = PrimaryCache.Get <PrimaryClassWithUniformPrimaryFromTheMapping>();

            // Assert
            Assert.IsNotNull(primary);
        }
Пример #13
0
        public void TestPrimaryCacheForTypeOfDerivedClass()
        {
            // Act
            var primary = PrimaryCache.Get(typeof(DerivedClassWithPrimaryAtBase));

            // Assert
            Assert.IsNotNull(primary);
        }
Пример #14
0
 public Cache(Func <IEnumerable <T> > initialCacheFunc)
 {
     this.initialCacheFunc = initialCacheFunc;
     primaryCache          = new PrimaryCache <T>();
     secondaryCaches       = new SecondaryCaches <T>();
     cacheStorageStrategy  = CacheStorageStrategyManager.GetStrategy();
     cacheMissStrategy     = new DefaultCacheMissStrategy <T>();
 }
Пример #15
0
        public void TestClassMapPrimaryMappingWithMapAttribute()
        {
            // Act
            var actual   = PrimaryCache.Get <ClassMapperTestWithAttributesClass>();
            var expected = "Id";

            // Assert
            Assert.AreEqual(expected, actual.GetMappedName());
        }
Пример #16
0
        public void TestClassMapPrimaryMapping()
        {
            // Act
            var actual   = PrimaryCache.Get <ClassMapperTestClass>();
            var expected = "PrimaryId";

            // Assert
            Assert.AreEqual(expected, actual.GetMappedName());
        }
Пример #17
0
        public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
        {
            var item = PrimaryCache.AddOrGetExisting(key, value, policy, regionName);

            //queue up writing to secondary cache on another thread
            ThreadPool.QueueUserWorkItem(_ => SecondaryCache.AddOrGetExisting(key, PrimaryCache.Get(key), policy, regionName));

            return(item);
        }
Пример #18
0
        public void TestKeyAttribute()
        {
            // Act
            var actual   = PrimaryCache.Get <KeyAttributeTestClass>();
            var expected = "WhateverId";

            // Assert
            Assert.AreEqual(expected, actual.PropertyInfo.Name);
        }
Пример #19
0
        public override object AddOrGetExisting(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
        {
            var item = PrimaryCache.AddOrGetExisting(key, value, absoluteExpiration, regionName);

            //queue up writing to secondary cache on another thread
            ThreadPool.QueueUserWorkItem(_ => SecondaryCache.AddOrGetExisting(key, PrimaryCache.Get(key), absoluteExpiration, regionName));

            return(item);
        }
Пример #20
0
        public override CacheItem AddOrGetExisting(CacheItem value, CacheItemPolicy policy)
        {
            var item = PrimaryCache.AddOrGetExisting(value, policy);

            //queue up writing to secondary cache on another thread
            ThreadPool.QueueUserWorkItem(_ => SecondaryCache.AddOrGetExisting(PrimaryCache.GetCacheItem(value.Key), policy));

            return(item);
        }
Пример #21
0
        public void TestKeyAndPrimaryAttributeCollision()
        {
            // Act
            var actual   = PrimaryCache.Get <KeyAttributeCollisionTestClass>();
            var expected = "KeyId";

            // Assert
            Assert.AreEqual(expected, actual.PropertyInfo.Name);
        }
Пример #22
0
        public void TestClassMapPrimaryMappingOverride()
        {
            // Setup
            PrimaryMapper.Add <ClassMapperTestClass>(e => e.RowId, true);

            // Act
            var actual   = PrimaryCache.Get <ClassMapperTestClass>();
            var expected = "RowId";

            // Assert
            Assert.AreEqual(expected, actual.GetMappedName());
        }
Пример #23
0
 /// <summary>
 /// database import as an asynchronous operation.
 /// </summary>
 /// <param name="parameters">The parameters.</param>
 /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
 public async Task <ICollectionImportResult> DatabaseImportAsync(ModCollectionExporterParams parameters)
 {
     // Caching sucks in this ORM
     DbFieldCache.Flush();
     FieldCache.Flush();
     IdentityCache.Flush();
     PrimaryCache.Flush();
     if (await IsV4Async(parameters))
     {
         return(await DatabaseImportv3Async(parameters));
     }
     return(await DatabaseImportv2Async(parameters));
 }
Пример #24
0
        public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(IEnumerable <string> keys, string regionName = null)
        {
            if ((PrimaryCache.DefaultCacheCapabilities & DefaultCacheCapabilities.CacheEntryChangeMonitors) == DefaultCacheCapabilities.CacheEntryChangeMonitors)
            {
                return(PrimaryCache.CreateCacheEntryChangeMonitor(keys, regionName));
            }

            if ((SecondaryCache.DefaultCacheCapabilities & DefaultCacheCapabilities.CacheEntryChangeMonitors) == DefaultCacheCapabilities.CacheEntryChangeMonitors)
            {
                return(SecondaryCache.CreateCacheEntryChangeMonitor(keys, regionName));
            }

            throw new NotImplementedException();
        }
Пример #25
0
        /// <summary>
        /// Insert or update an entity record.
        /// </summary>
        /// <typeparam name="TEntity">The type being inserted.</typeparam>
        /// <param name="connectionInfo">Connection info</param>
        /// <param name="data">Entity record</param>
        /// <param name="expressions"></param>
        /// <param name="token">Cancellation token</param>
        /// <returns></returns>
        public async Task <TEntity> UpsertAsync <TEntity>(IConnectionInfo connectionInfo, TEntity data, Expression <Func <TEntity, object> >[] expressions = null, CancellationToken token = default) where TEntity : class, IEntity
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            await using var connection = connectionInfo.GetConnection <T>();
            var id = await connection.MergeAsync(data, cancellationToken : token).ConfigureAwait(false);

            var propertyInfo = PrimaryCache.Get <TEntity>().PropertyInfo;

            propertyInfo.SetValue(data, Convert.ChangeType(id, propertyInfo.PropertyType));
            return(data);
        }
Пример #26
0
        public override object Get(string key, string regionName = null)
        {
            var item = PrimaryCache.Get(key, regionName);

            if (item == null)
            {
                item = SecondaryCache.Get(key, regionName);

                if (item != null)
                {
                    PrimaryCache.Set(key, item, new CacheItemPolicy(), regionName);
                }
            }

            return(item);
        }
Пример #27
0
        public void Cleanup()
        {
            ClassMapper.Clear();
            PropertyMapper.Clear();
            PrimaryMapper.Clear();
            IdentityMapper.Clear();
            TypeMapper.Clear();
            //PropertyHandlerMapper.Clear();
            PropertyValueAttributeMapper.Clear();

            ClassMappedNameCache.Flush();
            PropertyCache.Flush();
            PrimaryCache.Flush();
            IdentityCache.Flush();
            TypeMapCache.Flush();
            //PropertyHandlerMapper.Clear();
            PropertyValueAttributeCache.Flush();
        }
Пример #28
0
        internal void BuildCache()
        {
            if (cacheIsBuilt)
            {
                return;
            }

            if (cacheStorageStrategy.HasExpired <T>())
            {
                var data = initialCacheFunc().ToList();
                InitializeCache(data);
            }
            else
            {
                primaryCache    = cacheStorageStrategy.Load <PrimaryCache <T> >("Primary");
                secondaryCaches = cacheStorageStrategy.Load <SecondaryCaches <T> >("Secondary");
            }

            cacheIsBuilt = true;
        }
Пример #29
0
        public void TestPrimaryMapperMappingViaExpressionWithMapAttribute()
        {
            // Setup
            PrimaryMapper.Add <PrimaryMapperTestWithAttributeClass>(e => e.ColumnInt);

            // Act
            var actual   = PrimaryMapper.Get <PrimaryMapperTestWithAttributeClass>();
            var expected = "ColumnInt";

            // Assert
            Assert.IsTrue(actual?.IsPrimary() == true);
            Assert.AreEqual(expected, actual?.GetMappedName());

            // Act
            actual   = PrimaryCache.Get <PrimaryMapperTestWithAttributeClass>();
            expected = "ColumnString";

            // Assert
            Assert.IsTrue(actual?.IsPrimary() == true);
            Assert.AreEqual(expected, actual?.GetMappedName());
        }
Пример #30
0
        /// <summary>
        /// gets image from tile server
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="pos"></param>
        /// <param name="zoom"></param>
        /// <returns></returns>
        public PureImage GetImageFrom(GMapProvider provider, GPoint pos, int zoom, out Exception result)
        {
            PureImage ret = null;

            result = null;

            try
            {
                var rtile = new RawTile(provider.DbId, pos, zoom);

                // let't check memmory first
                if (UseMemoryCache)
                {
                    var m = MemoryCache.GetTileFromMemoryCache(rtile);
                    if (m != null)
                    {
                        if (GMapProvider.TileImageProxy != null)
                        {
                            ret = GMapProvider.TileImageProxy.FromArray(m);
                            if (ret == null)
                            {
#if DEBUG
                                Debug.WriteLine("Image disposed in MemoryCache o.O, should never happen ;} " + new RawTile(provider.DbId, pos, zoom));
                                if (Debugger.IsAttached)
                                {
                                    Debugger.Break();
                                }
#endif
                                m = null;
                            }
                        }
                    }
                }

                if (ret == null)
                {
                    if (Mode != AccessMode.ServerOnly && !provider.BypassCache)
                    {
                        if (PrimaryCache != null)
                        {
                            // hold writer for 5s
                            if (cacheOnIdleRead)
                            {
                                Interlocked.Exchange(ref readingCache, 5);
                            }

                            ret = PrimaryCache.GetImageFromCache(provider.DbId, pos, zoom);
                            if (ret != null)
                            {
                                if (UseMemoryCache)
                                {
                                    MemoryCache.AddTileToMemoryCache(rtile, ret.Data.GetBuffer());
                                }
                                return(ret);
                            }
                        }

                        if (SecondaryCache != null)
                        {
                            // hold writer for 5s
                            if (cacheOnIdleRead)
                            {
                                Interlocked.Exchange(ref readingCache, 5);
                            }

                            ret = SecondaryCache.GetImageFromCache(provider.DbId, pos, zoom);
                            if (ret != null)
                            {
                                if (UseMemoryCache)
                                {
                                    MemoryCache.AddTileToMemoryCache(rtile, ret.Data.GetBuffer());
                                }
                                EnqueueCacheTask(new CacheQueueItem(rtile, ret.Data.GetBuffer(), CacheUsage.First));
                                return(ret);
                            }
                        }
                    }

                    if (Mode != AccessMode.CacheOnly)
                    {
                        ret = provider.GetTileImage(pos, zoom);
                        {
                            // Enqueue Cache
                            if (ret != null)
                            {
                                if (UseMemoryCache)
                                {
                                    MemoryCache.AddTileToMemoryCache(rtile, ret.Data.GetBuffer());
                                }

                                if (Mode != AccessMode.ServerOnly && !provider.BypassCache)
                                {
                                    EnqueueCacheTask(new CacheQueueItem(rtile, ret.Data.GetBuffer(), CacheUsage.Both));
                                }
                            }
                        }
                    }
                    else
                    {
                        result = noDataException;
                    }
                }
            }
            catch (Exception ex)
            {
                result = ex;
                ret    = null;
                Debug.WriteLine("GetImageFrom: " + ex.ToString());
            }

            return(ret);
        }