Ejemplo n.º 1
0
        public static CacheKey GetRoleCacheKey(string roleId)
        {
            var roleKey = new CacheKey();

            roleKey.AddName("Role", roleId);
            return(roleKey);
        }
Ejemplo n.º 2
0
        public static CacheKey GetUserCacheKey(string userId)
        {
            var cacheKey = new CacheKey();

            cacheKey.AddName("LoginUser", userId);
            return(cacheKey);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取授权操作缓存键值
        /// </summary>
        /// <param name="operationKey">授权操作值</param>
        /// <returns></returns>
        public static CacheKey GetOperationCacheKey(string operationKey)
        {
            CacheKey cacheKey = new CacheKey();

            cacheKey.AddName("AuthOperation", operationKey);
            return(cacheKey);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取操作分组缓存键值
        /// </summary>
        /// <param name="groupKey"></param>
        /// <returns></returns>
        public static CacheKey GetOperationGroupCacheKey(string groupKey)
        {
            var groupCacheKey = new CacheKey();

            groupCacheKey.AddName("AuthOperationGroup", groupKey);
            return(groupCacheKey);
        }
Ejemplo n.º 5
0
        public static CacheKey GetUserRoleCacheKey(string userId)
        {
            var cacheKey = new CacheKey();

            cacheKey.AddName("UserRole", userId);
            return(cacheKey);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 获取用户授权缓存键值
        /// </summary>
        /// <param name="userIdentityKey">用户标识值</param>
        /// <returns></returns>
        public static CacheKey GetUserAuthOperationCacheKey(string userIdentityKey)
        {
            CacheKey cacheKey = new CacheKey();

            cacheKey.AddName("UserAuthOperation", userIdentityKey);
            return(cacheKey);
        }
Ejemplo n.º 7
0
 static CacheUtil()
 {
     AllLoginUserCacheKey = new CacheKey();
     AllLoginUserCacheKey.AddName("AllLoginUser");
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Get cache datas by condition
        /// </summary>
        /// <typeparam name="T">Data type</typeparam>
        /// <param name="query">Query condition</param>
        /// <param name="size">Return data size</param>
        /// <returns></returns>
        protected virtual QueryDataResult <T> GetCacheDatasByCondition <T>(IQuery query, int size)
        {
            Type entityType          = typeof(T);
            var  entityConfiguration = EntityManager.GetEntityConfiguration(entityType);

            if (entityConfiguration == null)
            {
                LogManager.LogError <DefaultDataCachePolicy>($"Entity :{entityType.FullName} configuration is null");
                return(QueryDataResult <T> .Default());
            }
            var primaryKeys = entityConfiguration.PrimaryKeys;

            if (primaryKeys.IsNullOrEmpty())
            {
                LogManager.LogError <T>($"Type:{entityType.FullName} no primary key is set,unable to get cache data");
                return(new QueryDataResult <T>()
                {
                    Datas = new List <T>(0),
                    QueryDatabase = true
                });
            }
            var otherKeys   = query.AllConditionFieldNames;
            var cacheObject = new CacheObject()
            {
                ObjectName = entityType.Name
            };

            #region cache prefix keys

            List <CacheKey> prefixDataKeys  = new List <CacheKey>();
            var             cachePrefixKeys = entityConfiguration.CachePrefixKeys ?? new List <string>(0);
            var             prefixKeyValues = query.GetKeysEqualValue(cachePrefixKeys);
            if (prefixKeyValues.Count != cachePrefixKeys.Count)
            {
                LogManager.LogError <T>($"Type:{entityType.FullName} miss cache prefix key values in IQuery");
                return(new QueryDataResult <T>()
                {
                    Datas = new List <T>(0),
                    QueryDatabase = true
                });
            }
            int preIndex = 0;
            foreach (var valItem in prefixKeyValues)
            {
                foreach (var prefixVal in valItem.Value)
                {
                    if (preIndex == 0)
                    {
                        var prefixDataKey = new CacheKey();
                        prefixDataKey.AddName(valItem.Key, prefixVal?.ToString() ?? string.Empty);
                        prefixDataKeys.Add(prefixDataKey);
                    }
                    else
                    {
                        foreach (var pdk in prefixDataKeys)
                        {
                            pdk.AddName(valItem.Key, prefixVal?.ToString() ?? string.Empty);
                        }
                    }
                }
                preIndex++;
            }
            otherKeys = otherKeys.Except(cachePrefixKeys);

            #endregion

            List <CacheKey> dataCacheKeys = new List <CacheKey>(otherKeys.Count());

            #region cache ignore keys

            var cacheIgnoreKeys = entityConfiguration.CacheIgnoreKeys ?? new List <string>(0);
            otherKeys = otherKeys.Except(cacheIgnoreKeys).ToList();

            #endregion

            #region primary keys

            var  primaryKeyValues = query.GetKeysEqualValue(primaryKeys);
            bool fullPrimaryKey   = primaryKeyValues.Count == primaryKeys.Count;
            if (fullPrimaryKey)
            {
                otherKeys = otherKeys.Except(primaryKeys).ToList();
                List <CacheKey> primaryCacheKeys = new List <CacheKey>();
                int             pindex           = 0;
                foreach (var valueItem in primaryKeyValues)
                {
                    if (valueItem.Value.IsNullOrEmpty())
                    {
                        continue;
                    }
                    foreach (var primaryVal in valueItem.Value)
                    {
                        if (pindex == 0)
                        {
                            var primaryCacheKey = new CacheKey(cacheObject, prefixDataKeys);
                            primaryCacheKey.AddName(valueItem.Key, primaryVal?.ToString() ?? string.Empty);
                            primaryCacheKeys.Add(primaryCacheKey);
                        }
                        else
                        {
                            foreach (var cacheKey in primaryCacheKeys)
                            {
                                cacheKey.AddName(valueItem.Key, primaryVal?.ToString() ?? string.Empty);
                            }
                        }
                    }
                    pindex++;
                }
                dataCacheKeys.AddRange(primaryCacheKeys);
            }

            #endregion

            #region cache fields

            var             cacheFields         = entityConfiguration.CacheKeys ?? new List <string>(0);
            List <CacheKey> cacheFieldCacheKeys = null;
            if (!cacheFields.IsNullOrEmpty())
            {
                var dataCacheFieldValues = query.GetKeysEqualValue(cacheFields);
                if (!dataCacheFieldValues.IsNullOrEmpty())
                {
                    otherKeys           = otherKeys.Except(dataCacheFieldValues.Keys).ToList();
                    cacheFieldCacheKeys = new List <CacheKey>();
                    foreach (var valueItem in dataCacheFieldValues)
                    {
                        if (valueItem.Value.IsNullOrEmpty())
                        {
                            continue;
                        }
                        foreach (var val in valueItem.Value)
                        {
                            var cacheKey = new CacheKey(cacheObject, prefixDataKeys);
                            cacheKey.AddName(valueItem.Key, val?.ToString() ?? string.Empty);
                            cacheFieldCacheKeys.Add(cacheKey);
                        }
                    }
                    dataCacheKeys.AddRange(CacheManager.String.Get(cacheFieldCacheKeys, cacheObject).Select(c => ConstantCacheKey.Create(c)));
                }
            }

            #endregion

            bool needSort = query != null && !query.Orders.IsNullOrEmpty();
            if (dataCacheKeys.IsNullOrEmpty() || (!otherKeys.IsNullOrEmpty() && needSort))
            {
                LogManager.LogInformation <DefaultDataCachePolicy>($"Type:{entityType.FullName},IQuery has any other criterias without cache keys and need sort");
                return(new QueryDataResult <T>()
                {
                    QueryDatabase = true,
                    Datas = new List <T>(0)
                });
            }
            dataCacheKeys = dataCacheKeys.Distinct().ToList();
            int      removeCount        = 0;
            var      queryConditionFunc = query.GetQueryExpression <T>(); //query condition
            List <T> dataList           = new List <T>();
            size = size < 0 ? 0 : size;                                   //return value count
            bool notQueryDb = false;
            CacheManager.GetDataList <T>(dataCacheKeys, cacheObject).ForEach(data =>
            {
                if (data != null && (queryConditionFunc?.Invoke(data) ?? true))
                {
                    dataList.Add(data);
                }
                else
                {
                    removeCount += data != null || DataCacheManager.Configuration.EnableCacheNullData ? 1 : 0;
                }
            });
            if (otherKeys.IsNullOrEmpty())
            {
                size       = size <= 0 ? dataCacheKeys.Count : (size > dataCacheKeys.Count ? dataCacheKeys.Count : size);
                notQueryDb = dataList.Count >= size - removeCount;
                if (notQueryDb && needSort)
                {
                    dataList = query.Sort(dataList).ToList();
                }
            }
            else
            {
                notQueryDb = size > 0 && dataList.Count >= size;
            }
            return(new QueryDataResult <T>()
            {
                QueryDatabase = !notQueryDb,
                QuriedCache = true,
                PrimaryCacheKeys = dataCacheKeys,
                OtherCacheKeys = cacheFieldCacheKeys,
                Datas = dataList
            });
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Get cache datas by type
        /// </summary>
        /// <typeparam name="T">Data type</typeparam>
        /// <param name="size">Return data size</param>
        /// <returns></returns>
        protected virtual List <T> GetCacheDatasByType <T>(IQuery query, int size)
        {
            var type     = typeof(T);
            var needSort = query != null && !query.Orders.IsNullOrEmpty();

            if (needSort)
            {
                return(new List <T>(0));
            }

            #region get cache keys

            var typeName        = type.Name;
            var primaryKeys     = EntityManager.GetPrimaryKeys(type);
            var firstPrimaryKey = primaryKeys?.FirstOrDefault();
            if (string.IsNullOrWhiteSpace(firstPrimaryKey))
            {
                return(new List <T>(0));
            }
            var cacheObject = new CacheObject()
            {
                ObjectName = typeName
            };
            CacheKey mateKey = new CacheKey(cacheObject);
            mateKey.AddName(firstPrimaryKey);
            var getKeysCommand = new GetKeysOptions()
            {
                CacheObject = cacheObject,
                Query       = new KeyQuery()
                {
                    MateKey  = mateKey.GetActualKey(),
                    Type     = KeyMatchPattern.StartWith,
                    Page     = 1,
                    PageSize = size > 0 ? size : int.MaxValue
                }
            };
            var keyResponses = CacheManager.Keys.GetKeysAsync(getKeysCommand).Result?.Responses;
            if (keyResponses.IsNullOrEmpty())
            {
                return(new List <T>(0));
            }
            List <CacheKey> dataKeys = new List <CacheKey>();
            foreach (var response in keyResponses)
            {
                if (response?.Keys.IsNullOrEmpty() ?? true)
                {
                    continue;
                }
                bool fullData = false;
                foreach (var key in response.Keys)
                {
                    if (size > 0 && dataKeys.Count >= size)
                    {
                        fullData = true;
                        break;
                    }
                    dataKeys.Add(key);
                }
                if (fullData)
                {
                    break;
                }
            }
            if (dataKeys.IsNullOrEmpty())
            {
                return(new List <T>(0));
            }

            #endregion

            #region get cache data

            return(CacheManager.GetDataList <T>(dataKeys, cacheObject));

            #endregion
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Remove cache data
        /// </summary>
        /// <typeparam name="T">Data type</typeparam>
        /// <param name="datas">Data list</param>
        protected virtual void RemoveCacheData <T>(IEnumerable <T> datas) where T : BaseEntity <T>, new()
        {
            if (datas.IsNullOrEmpty())
            {
                return;
            }
            Type entityType          = typeof(T);
            var  entityConfiguration = EntityManager.GetEntityConfiguration(entityType);

            if (entityConfiguration == null)
            {
                LogManager.LogError <DefaultDataCachePolicy>($"Entity :{entityType.FullName} configuration is null");
                return;
            }
            string objectName = entityType.Name;

            //primary keys
            var primaryKeys = entityConfiguration.PrimaryKeys;

            if (primaryKeys.IsNullOrEmpty())
            {
                LogManager.LogError <DefaultDataCachePolicy>($"Type:{entityType.FullName} no primary key is set,unable to remove cache data");
                return;
            }

            //cache prefix keys
            var cachePrefixKeys = entityConfiguration.CachePrefixKeys ?? new List <string>(0);

            //cache keys
            var cacheKeys   = entityConfiguration.CacheKeys ?? new List <string>(0);
            var cacheObject = new CacheObject()
            {
                ObjectName = objectName
            };
            List <CacheKey> cacheOptionKeys = new List <CacheKey>();

            foreach (var data in datas)
            {
                //prefix cache keys
                var dataPrefixKey = new CacheKey();
                if (!cachePrefixKeys.IsNullOrEmpty())
                {
                    foreach (var preKey in cachePrefixKeys)
                    {
                        var preKeyVal = data.GetValue(preKey)?.ToString() ?? string.Empty;
                        dataPrefixKey.AddName(preKey, preKeyVal);
                    }
                }

                //data cache key
                var dataCacheKey = new CacheKey(cacheObject, dataPrefixKey);
                foreach (string pk in primaryKeys)
                {
                    var pkValue = data.GetValue(pk);
                    dataCacheKey.AddName(pk, pkValue == null ? string.Empty : pkValue.ToString());
                }
                cacheOptionKeys.Add(dataCacheKey);

                //cache keys
                if (!cacheKeys.IsNullOrEmpty())
                {
                    foreach (string key in cacheKeys)
                    {
                        var otherCacheKey = new CacheKey(cacheObject, dataPrefixKey);
                        var cacheKeyValue = data.GetValue(key);
                        otherCacheKey.AddName(key, cacheKeyValue == null ? string.Empty : cacheKeyValue.ToString());
                        cacheOptionKeys.Add(otherCacheKey);
                    }
                }
            }
            if (cacheOptionKeys.IsNullOrEmpty())
            {
                return;
            }
            DeleteOptions removeCommand = new DeleteOptions()
            {
                CacheObject = cacheObject,
                Keys        = cacheOptionKeys
            };
            var result = CacheManager.Keys.Delete(removeCommand);

            if (result != null && !result.Responses.IsNullOrEmpty())
            {
                foreach (var response in result.Responses)
                {
                    if (!string.IsNullOrWhiteSpace(response?.Message))
                    {
                        LogManager.LogInformation <DefaultDataCachePolicy>(response.Message);
                    }
                }
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Add datas
        /// </summary>
        /// <typeparam name="T">Data type</typeparam>
        /// <param name="query">Query condition</param>
        /// <param name="datas">Datas</param>
        protected virtual void AddCacheData <T>(QueryDataCallbackContext <T> queryDataCallbackContext) where T : BaseEntity <T>, new()
        {
            if (queryDataCallbackContext == null)
            {
                return;
            }

            #region Add cache data

            var               datas           = queryDataCallbackContext.Datas;
            List <CacheKey>   dataPrimaryKeys = null;
            List <CacheKey>   dataOtherKeys   = null;
            List <CacheEntry> storeItems      = new List <CacheEntry>();
            Type              entityType      = typeof(T);
            string            objectName      = entityType.Name;
            var               cacheObject     = new CacheObject()
            {
                ObjectName = objectName
            };
            int dataCount = 0;
            if (!datas.IsNullOrEmpty())
            {
                dataPrimaryKeys = new List <CacheKey>();
                dataOtherKeys   = new List <CacheKey>();
                var entityConfiguration = EntityManager.GetEntityConfiguration(entityType);
                if (entityConfiguration == null)
                {
                    LogManager.LogError <DefaultDataCachePolicy>($"Entity :{entityType.FullName} configuration is null");
                    return;
                }

                //primary keys
                var primaryKeys = entityConfiguration.PrimaryKeys;
                if (primaryKeys.IsNullOrEmpty())
                {
                    LogManager.LogError <DefaultDataCachePolicy>($"Data type:{entityType.FullName} no primary key,unable to set cache data");
                    return;
                }

                //cache keys
                var cacheKeys = entityConfiguration.CacheKeys ?? new List <string>(0);
                cacheKeys = cacheKeys.Except(primaryKeys).ToList();

                //cache prefix keys
                var cachePrefixKeys = entityConfiguration.CachePrefixKeys ?? new List <string>(0);
                foreach (var data in datas)
                {
                    if (data == null)
                    {
                        continue;
                    }
                    dataCount++;
                    bool keyValueSuccess = true;
                    //expiration
                    TimeSpan?      dataExpirationValue = DataCacheManager.Configuration.GetExpiration(entityType);
                    DateTimeOffset?dataExpirationTime  = null;
                    if (dataExpirationValue != null)
                    {
                        dataExpirationTime = DateTimeOffset.Now.Add(dataExpirationValue.Value).AddSeconds(randomSecondPrivider.TakeNextValues(1).FirstOrDefault());
                    }
                    CacheExpiration dataExpiration = new CacheExpiration()
                    {
                        SlidingExpiration  = false,
                        AbsoluteExpiration = dataExpirationTime
                    };
                    //prefix cache keys
                    var dataPrefixKey = new CacheKey();
                    if (!cachePrefixKeys.IsNullOrEmpty())
                    {
                        foreach (var preKey in cachePrefixKeys)
                        {
                            var preKeyVal = data.GetValue(preKey)?.ToString() ?? string.Empty;
                            if (string.IsNullOrWhiteSpace(preKeyVal))
                            {
                                LogManager.LogError <DefaultDataCachePolicy>($"Data type :{entityType.FullName},Identity value:{data.GetIdentityValue()},Cache prefix key:{preKey},value is null or empty,unable to set cache data");
                                keyValueSuccess = false;
                                break;
                            }
                            dataPrefixKey.AddName(preKey, preKeyVal);
                        }
                        if (!keyValueSuccess)
                        {
                            continue;
                        }
                    }
                    //primary data cache keys
                    var dataCacheKey = new CacheKey(cacheObject, dataPrefixKey);
                    foreach (string pk in primaryKeys)
                    {
                        var pkValue = data.GetValue(pk)?.ToString() ?? string.Empty;
                        if (string.IsNullOrWhiteSpace(pkValue))
                        {
                            LogManager.LogError <DefaultDataCachePolicy>($"Data type :{entityType.FullName},Identity value:{data.GetIdentityValue()},Primary key:{pk},value is null or empty,unable to set cache data");
                            keyValueSuccess = false;
                            break;
                        }
                        dataCacheKey.AddName(pk, pkValue);
                    }
                    if (!keyValueSuccess)
                    {
                        continue;
                    }
                    string primaryFullCacheKey = dataCacheKey.GetActualKey();
                    if (primaryFullCacheKey.IsNullOrEmpty())
                    {
                        continue;
                    }
                    dataPrimaryKeys.Add(primaryFullCacheKey);
                    storeItems.Add(new CacheEntry()
                    {
                        Key        = primaryFullCacheKey,
                        Value      = JsonSerializeHelper.ObjectToJson(data),
                        Expiration = dataExpiration
                    });
                    if (!cacheKeys.IsNullOrEmpty())
                    {
                        foreach (string key in cacheKeys)
                        {
                            var otherCacheKey = new CacheKey(cacheObject, dataPrefixKey);
                            var cacheKeyValue = data.GetValue(key)?.ToString() ?? string.Empty;
                            if (string.IsNullOrWhiteSpace(cacheKeyValue))
                            {
                                LogManager.LogError <DefaultDataCachePolicy>($"Data type :{entityType.FullName},Identity value:{data.GetIdentityValue()},Cache key:{key},value is null or empty,unable to set cache data");
                                keyValueSuccess = false;
                                break;
                            }
                            otherCacheKey.AddName(key, cacheKeyValue?.ToString() ?? string.Empty);
                            dataOtherKeys.Add(otherCacheKey);
                            storeItems.Add(new CacheEntry()
                            {
                                Key        = otherCacheKey.GetActualKey(),
                                Value      = primaryFullCacheKey,
                                Expiration = dataExpiration
                            });
                        }
                        if (!keyValueSuccess)
                        {
                            continue;
                        }
                    }
                }
            }

            #endregion

            #region Null data

            int querySize = queryDataCallbackContext.Query?.QuerySize ?? 0;
            if (DataCacheManager.Configuration.EnableCacheNullData && (querySize < 1 || dataCount < querySize))
            {
                IEnumerable <CacheKey> queryPrimaryKeys = queryDataCallbackContext.PrimaryCacheKeys;
                IEnumerable <CacheKey> queryOtherKeys   = queryDataCallbackContext.OtherCacheKeys;
                string         nullDataValue            = JsonSerializeHelper.ObjectToJson <T>(null);
                TimeSpan?      nullDataExpirationValue  = DataCacheManager.Configuration.GetNullDataExpiration(entityType);
                DateTimeOffset?nullDataExpiration       = null;
                if (nullDataExpirationValue != null)
                {
                    nullDataExpiration = DateTimeOffset.Now.Add(nullDataExpirationValue.Value);
                }
                CacheExpiration nullDataExp = new CacheExpiration()
                {
                    SlidingExpiration  = false,
                    AbsoluteExpiration = nullDataExpiration
                };
                if (!queryPrimaryKeys.IsNullOrEmpty())
                {
                    if (!dataPrimaryKeys.IsNullOrEmpty())
                    {
                        queryPrimaryKeys = queryPrimaryKeys.Except(dataPrimaryKeys);
                    }
                    foreach (var primaryKey in queryPrimaryKeys)
                    {
                        storeItems.Add(new CacheEntry()
                        {
                            Key        = primaryKey.GetActualKey(),
                            Value      = nullDataValue,
                            Expiration = nullDataExp
                        });
                    }
                }
                if (!queryOtherKeys.IsNullOrEmpty())
                {
                    if (!dataOtherKeys.IsNullOrEmpty())
                    {
                        queryOtherKeys = queryOtherKeys.Except(dataOtherKeys);
                    }
                    if (!queryOtherKeys.IsNullOrEmpty())
                    {
                        storeItems.Add(new CacheEntry()
                        {
                            Key        = DataCacheManager.Configuration.NullDataCacheKey,
                            Value      = nullDataValue,
                            Expiration = nullDataExp
                        });
                        foreach (var otherKey in queryOtherKeys)
                        {
                            storeItems.Add(new CacheEntry()
                            {
                                Key        = otherKey,
                                Value      = DataCacheManager.Configuration.NullDataCacheKey,
                                Expiration = nullDataExp
                            });
                        }
                    }
                }
            }

            #endregion

            StringSetOptions option = new StringSetOptions()
            {
                CacheObject  = cacheObject,
                Items        = storeItems,
                CommandFlags = CacheCommandFlags.FireAndForget
            };
            var cacheResult = CacheManager.String.Set(option);
            if (cacheResult != null && !cacheResult.Responses.IsNullOrEmpty())
            {
                foreach (var response in cacheResult.Responses)
                {
                    if (!string.IsNullOrWhiteSpace(response?.Message))
                    {
                        LogManager.LogInformation <DefaultDataCachePolicy>(response.Message);
                    }
                }
            }
        }