Пример #1
0
        public void Load(string service, string method, string query, CacheObjectType type)
        {
            Log.Debug($"Loading cache data for {service}::{method} of type {type}");

            // if the cache already exists do not generate it again!
            if (this.Exists(service, method) == true)
            {
                return;
            }

            try
            {
                MySqlConnection connection  = null;
                MySqlDataReader reader      = Database.Query(ref connection, query);
                PyDataType      cacheObject = null;

                using (connection)
                    using (reader)
                    {
                        switch (type)
                        {
                        case CacheObjectType.Rowset:
                            cacheObject = Rowset.FromMySqlDataReader(reader);
                            break;

                        case CacheObjectType.CRowset:
                            cacheObject = CRowset.FromMySqlDataReader(reader);
                            break;

                        case CacheObjectType.TupleSet:
                            cacheObject = TupleSet.FromMySqlDataReader(reader);
                            break;

                        case CacheObjectType.PackedRowList:
                            cacheObject = PyPackedRowList.FromMySqlDataReader(reader);
                            break;

                        case CacheObjectType.IntIntDict:
                            cacheObject = IntIntDictionary.FromMySqlDataReader(reader);
                            break;

                        case CacheObjectType.IndexRowset:
                            cacheObject = IndexRowset.FromMySqlDataReader(reader, 0);
                            break;
                        }

                        StoreCall(service, method, cacheObject, DateTime.UtcNow.ToFileTimeUtc());
                    }
            }
            catch (Exception)
            {
                Log.Error($"Cannot generate cache data for {service}::{method}");
                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// Helper method that queries the cached object and converts it to the proper type to be stored
        /// </summary>
        /// <param name="query">The query to run</param>
        /// <param name="type">The type of object to store</param>
        /// <returns>The final object to be used by the cache</returns>
        private PyDataType QueryCacheObject(string query, CacheObjectType type)
        {
            MySqlConnection connection = null;
            MySqlDataReader reader     = Database.Query(ref connection, query);

            using (connection)
                using (reader)
                {
                    return(type switch
                    {
                        CacheObjectType.Rowset => Rowset.FromMySqlDataReader(Database, reader),
                        CacheObjectType.CRowset => CRowset.FromMySqlDataReader(Database, reader),
                        CacheObjectType.TupleSet => TupleSet.FromMySqlDataReader(Database, reader),
                        CacheObjectType.PackedRowList => PyPackedRowList.FromMySqlDataReader(Database, reader),
                        CacheObjectType.IntIntDict => IntIntDictionary.FromMySqlDataReader(reader),
                        CacheObjectType.IndexRowset => IndexRowset.FromMySqlDataReader(Database, reader, 0),
                        _ => null
                    });
Пример #3
0
        private void Load(string name, string query, CacheObjectType type)
        {
            Log.Debug($"Loading cache data for {name} of type {type}");

            try
            {
                MySqlConnection connection  = null;
                MySqlDataReader reader      = Database.Query(ref connection, query);
                PyDataType      cacheObject = null;

                using (connection)
                    using (reader)
                    {
                        switch (type)
                        {
                        case CacheObjectType.Rowset:
                            cacheObject = Rowset.FromMySqlDataReader(reader);
                            break;

                        case CacheObjectType.CRowset:
                            cacheObject = CRowset.FromMySqlDataReader(reader);
                            break;

                        case CacheObjectType.TupleSet:
                            cacheObject = TupleSet.FromMySqlDataReader(reader);
                            break;

                        case CacheObjectType.PackedRowList:
                            cacheObject = PyPackedRowList.FromMySqlDataReader(reader);
                            break;
                        }

                        Store(name, cacheObject, DateTime.Now.ToFileTimeUtc());
                    }
            }
            catch (Exception e)
            {
                Log.Error($"Cannot generate cache data for {name}");
                throw;
            }
        }
Пример #4
0
 public CacheObject(string name, CacheObjectType type, object value)
 {
     this.Name = name;
     this.Type = type;
     switch (type)
     {
         case CacheObjectType.String:
             this.Value = value;
             this.Size = (uint)Encoding.GetBytes((string)value).Length;
             break;
         case CacheObjectType.Byte:
             this.Value = value;
             this.Size = 1;
             break;
         case CacheObjectType.SByte:
             this.Value = value;
             this.Size = 1;
             break;
         case CacheObjectType.Bool:
             this.Value = value;
             this.Size = 1;
             break;
         case CacheObjectType.UShort:
             this.Value = value;
             this.Size = 2;
             break;
         case CacheObjectType.Short:
             this.Value = value;
             this.Size = 2;
             break;
         case CacheObjectType.UInt:
             this.Value = value;
             this.Size = 4;
             break;
         case CacheObjectType.Int:
             this.Value = value;
             this.Size = 4;
             break;
         case CacheObjectType.ULong:
             this.Value = value;
             this.Size = 8;
             break;
         case CacheObjectType.Long:
             this.Value = value;
             this.Size = 8;
             break;
         case CacheObjectType.Float:
             this.Value = value;
             this.Size = 4;
             break;
         case CacheObjectType.Double:
             this.Value = value;
             this.Size = 8;
             break;
         case CacheObjectType.Decimal:
             this.Value = value;
             this.Size = 16;
             break;
         case CacheObjectType.Data:
             this.Value = value;
             this.Size = (uint)((byte[])value).Length;
             break;
         default:
             throw new Exception("Unknown cache object type!");
     }
 }
Пример #5
0
 /// <summary>
 /// Returns
 /// </summary>
 /// <param name="companyId"></param>
 /// <param name="projectId"></param>
 /// <param name="revisionId"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 internal static string GenerateCacheKey(int companyId, int projectId, int revisionId, CacheObjectType type)
 {
     return($"cId:{companyId}_pId:{projectId}_rId:{revisionId}_t:{type.ToString()}");
 }
Пример #6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="companyId"></param>
 /// <param name="projectId"></param>
 /// <param name="revisionId"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool IsIncache(int companyId, int projectId, int revisionId, CacheObjectType type)
 {
     return(IsIncache(GenerateCacheKey(companyId, projectId, revisionId, type)));
 }
Пример #7
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="companyId"></param>
 /// <param name="projectId"></param>
 /// <param name="revisionId"></param>
 /// <param name="type"></param>
 public static void RemoveFromCache(int companyId, int projectId, int revisionId, CacheObjectType type)
 {
     RemoveFromCache(GenerateCacheKey(companyId, projectId, revisionId, type));
 }
Пример #8
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="companyId"></param>
 /// <param name="projectId"></param>
 /// <param name="revisionId"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static T GetFromCache <T>(int companyId, int projectId, int revisionId, CacheObjectType type) where T : class
 {
     return(MemoryCache.Default[GenerateCacheKey(companyId, projectId, revisionId, type)] as T);
 }
Пример #9
0
 /// <summary>
 /// Saves an Object To Cache
 /// </summary>
 /// <param name="companyId"></param>
 /// <param name="projectId"></param>
 /// <param name="revisionId"></param>
 /// <param name="saveItem"></param>
 /// <param name="type"></param>
 public static void SaveToCache(int companyId, int projectId, int revisionId, object saveItem, CacheObjectType type)
 {
     SaveTocache(GenerateCacheKey(companyId, projectId, revisionId, type), saveItem, DateTime.UtcNow.Add(TimeSpan.FromHours(4)));
 }