Example #1
0
        private T DeserializeCacheData(AppCacheData cachedData)
        {
            T returnValue = default;

            if (cachedData.SerializeType == SerializeType.Json)
            {
                returnValue = cachedData.CachedData.JsonDeserialize <T>();
            }
            else if (cachedData.SerializeType == SerializeType.MessagePack)
            {
                var rawData = Convert.FromBase64String(cachedData.CachedData);
                returnValue = MessagePackSerializer.Deserialize <T>(rawData);
            }

            Debug.Assert(returnValue != null);

            return(returnValue);
        }
Example #2
0
        public async Task <T> GetObject()
        {
            var sqliteService = ShinyHost.Resolve <ISQLiteService>();

            var cachedData = await sqliteService.SelectAsync <AppCacheData>(this.CacheKey);

            if (cachedData == null || cachedData?.ExpireTime < DateTime.UtcNow)
            {
                var loadedData = await Loader.Invoke();

                if (loadedData == null)
                {
                    return(null);
                }

                if (ExpireTime != TimeSpan.Zero)
                {
                    cachedData = new AppCacheData();
                    cachedData.BindCacheData(loadedData, CacheKey, ExpireTime, SerializeType);
                    await sqliteService.InsertOrUpdateAsync(cachedData);
                }
                else
                {
                    if (SerializeType == SerializeType.Json)
                    {
                        return((loadedData as string).JsonDeserialize <T>());
                    }
                    else if (SerializeType == SerializeType.MessagePack)
                    {
                        return(MessagePackSerializer.Deserialize <T>((loadedData as byte[])));
                    }
                }
            }

            return(DeserializeCacheData(cachedData));
        }