Пример #1
0
        private string GenerateKey(SqlStatement <T> statement)
        {
            //use the type name of the mapped class as first key part
            //this will allow us to clear all objects in the cache with the same type
            var firstKeyPart = typeof(T).FullName;

            //hash the query and parameters
            var query = statement.GenerateSqlStatement();

            using (var md5 = System.Security.Cryptography.MD5.Create())
            {
                //hash query
                var hashedBytes   = md5.ComputeHash(Encoding.UTF8.GetBytes(query));
                var secondKeyPart = Convert.ToBase64String(hashedBytes);

                //hash the parameters
                var sb = new StringBuilder();
                foreach (var parameter in statement.Parameters)
                {
                    sb.Append(parameter.Name).Append(parameter.Value);
                }
                string parameters = sb.ToString();
                hashedBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(parameters));
                var thirdKeyPart = Convert.ToBase64String(hashedBytes);

                //build the key
                return($"{firstKeyPart}_{secondKeyPart}_{thirdKeyPart}");
            }
        }