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));
        }
        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));
        }
Exemple #3
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));
        }
Exemple #4
0
        public void TestPrimaryCacheForDerivedClassWithoutPrimary()
        {
            // Act
            var primary = PrimaryCache.Get <DerivedClass>();

            // Assert
            Assert.IsNull(primary);
        }
Exemple #5
0
        public void TestPrimaryCacheForTypeOfDerivedClass()
        {
            // Act
            var primary = PrimaryCache.Get(typeof(DerivedClassWithPrimaryAtBase));

            // Assert
            Assert.IsNotNull(primary);
        }
Exemple #6
0
        public void TestPrimaryCacheForBaseClass()
        {
            // Act
            var primary = PrimaryCache.Get <BaseClassWithPrimary>();

            // Assert
            Assert.IsNotNull(primary);
        }
Exemple #7
0
        public void TestPrimaryClass()
        {
            // Act
            var primary = PrimaryCache.Get <PrimaryClass>();

            // Assert
            Assert.IsNotNull(primary);
        }
Exemple #8
0
        public void TestPrimaryCacheForDerivedClass()
        {
            // Act
            var primary = PrimaryCache.Get <DerivedClassWithPrimaryAtBase>();

            // Assert
            Assert.IsNotNull(primary);
        }
Exemple #9
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);
        }
Exemple #10
0
        public void TestPrimaryCacheForTypeOfDerivedClassWithoutPrimary()
        {
            // Act
            var primary = PrimaryCache.Get(typeof(DerivedClass));

            // Assert
            Assert.IsNull(primary);
        }
Exemple #11
0
        public void TestPrimaryClassWithUniformPrimaryFromTheMapping()
        {
            // Act
            var primary = PrimaryCache.Get <PrimaryClassWithUniformPrimaryFromTheMapping>();

            // Assert
            Assert.IsNotNull(primary);
        }
Exemple #12
0
        public void TestClassMapPrimaryMappingWithMapAttribute()
        {
            // Act
            var actual   = PrimaryCache.Get <ClassMapperTestWithAttributesClass>();
            var expected = "Id";

            // Assert
            Assert.AreEqual(expected, actual.GetMappedName());
        }
Exemple #13
0
        public void TestClassMapPrimaryMapping()
        {
            // Act
            var actual   = PrimaryCache.Get <ClassMapperTestClass>();
            var expected = "PrimaryId";

            // Assert
            Assert.AreEqual(expected, actual.GetMappedName());
        }
        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);
        }
Exemple #15
0
        public void TestKeyAndPrimaryAttributeCollision()
        {
            // Act
            var actual   = PrimaryCache.Get <KeyAttributeCollisionTestClass>();
            var expected = "KeyId";

            // Assert
            Assert.AreEqual(expected, actual.PropertyInfo.Name);
        }
Exemple #16
0
        public void TestKeyAttribute()
        {
            // Act
            var actual   = PrimaryCache.Get <KeyAttributeTestClass>();
            var expected = "WhateverId";

            // Assert
            Assert.AreEqual(expected, actual.PropertyInfo.Name);
        }
        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);
        }
Exemple #18
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());
        }
Exemple #19
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);
        }
        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);
        }
Exemple #21
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());
        }