コード例 #1
0
        /// <summary>
        ///     Get from cache.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="region"></param>
        /// <returns>
        ///     An object instance with the Cache Value corresponding to the entry if found, else null
        /// </returns>
        public override async Task <object> Get(object cacheKey, string region)
        {
            if (!_isEnabled)
            {
                return(null);
            }

            object item;

            lock (Sync)
            {
                item = _cache.Get(MemoryUtilities.CombinedKey(cacheKey, region));
            }

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

            // todo remove after dec when obsolete methods are removed
            var itemType = item.GetType();

            if (itemType == typeof(BaseModel))
            {
                var obj = (BaseModel)item;
                return(await MemoryStreamHelper.DeserializeObject(obj.CacheObject));
            }

            if (itemType == typeof(CacheObject))
            {
                var obj1 = (CacheObject)item;
                return(await MemoryStreamHelper.DeserializeObject(obj1.Item));
            }
            return(null);
        }
コード例 #2
0
        public override async Task <bool> Add(object cacheKey, object cacheObject, string region, bool allowSliddingTime, int expirationInMinutes = 15)
        {
            if (!_isEnabled)
            {
                return(true);
            }

            var expireCacheTime = expirationInMinutes == 15 ? _cacheExpirationTime : expirationInMinutes;
            var item            = new CacheItem();
            var exist           = await GetItem(cacheKey, region);

            if (exist == null)
            {
                item.CacheKey = cacheKey.ToString();
            }
            else
            {
                item = exist;
            }

            var expireTime = DateTime.UtcNow.AddMinutes(expireCacheTime);

            item.Expires = expireTime;

            item.CacheObject = await MemoryStreamHelper.SerializeObject(cacheObject);

            item.AllowSliddingTime = allowSliddingTime;

            return(await CreateUpdateItem(region, item));
        }
コード例 #3
0
        /// <summary>
        ///     Get from cache.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="region"></param>
        /// <returns>
        ///     An object instance with the Cache Value corresponding to the entry if found, else null
        /// </returns>
        public override async Task <object> Get(object cacheKey, string region)
        {
            if (!_isEnabled)
            {
                return(null);
            }

            object item = await GetItem(cacheKey, region);

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

            // todo remove after dec when obsolete methods are removed
            var itemType = item.GetType();

            if (itemType == typeof(CacheItem))
            {
                var obj = (CacheItem)item;
                return(await MemoryStreamHelper.DeserializeObject(obj.CacheObject));
            }

            if (itemType == typeof(MongoCachModel))
            {
                var obj1 = (MongoCachModel)item;
                return(await MemoryStreamHelper.DeserializeObject(obj1.CacheObject.Item));
            }
            return(null);
        }
コード例 #4
0
        /// <summary>
        ///     Add an item to the cache and will need to be removed manually
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="cacheObject">The cache object.</param>
        /// <param name="region">If region is supported by cache , it will seperate the lookups</param>
        /// <param name="options">Options that can be set for the cache</param>
        /// <returns>true or false</returns>
        public override async Task <bool> AddPermanent(object cacheKey, object cacheObject, string region, ICacheOptions options)
        {
            if (!_isEnabled)
            {
                return(true);
            }

            if (await Get(cacheKey, region) != null)
            {
                await Remove(cacheKey, region);
            }

            var expireTime = DateTime.UtcNow.AddYears(100);
            var item       = new MongoCachModel
            {
                CacheKey    = cacheKey.ToString(),
                Expires     = expireTime,
                CacheObject = new CacheObject
                {
                    Item = await MemoryStreamHelper.SerializeObject(cacheObject)
                },
                CacheOptions = options
            };

            return(await CreateUpdateItem(region, item));
        }
コード例 #5
0
    protected void ASPxUploadControl_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
    {
        ASPxUploadControl uploadControl = (ASPxUploadControl)sender;
        UploadedFile      uploadedFile  = uploadControl.UploadedFiles[0];

        e.CallbackData = Request.Url.AbsoluteUri.Replace(Request.Url.Query, String.Empty) + String.Format("?loadFile={0}", uploadedFile.FileName);
        try {
            Stream pdfStream = ConvertToPdf(uploadedFile.FileContent, uploadedFile.FileName);
            Session["UploadedFile"] = MemoryStreamHelper.ToBytes(pdfStream);
        } catch (Exception ex) {
            e.ErrorText = ex.Message;
            e.IsValid   = false;
        }
    }
コード例 #6
0
        private static async Task <bool> CreateUpdateItem(object cacheKey, object cacheObject, string region, CacheItemPolicy cacheItemPolicy, ICacheOptions cacheOptions)
        {
            var key       = MemoryUtilities.CombinedKey(cacheKey, region);
            var cacheItem = new CacheObject
            {
                Item = await MemoryStreamHelper.SerializeObject(cacheObject)
            };

            bool results;

            lock (Sync)
            {
                results = _cache.Add(key, cacheItem, cacheItemPolicy);
            }

            return(results);
        }
コード例 #7
0
    private void WritePdfToResponse(string fileName)
    {
        object uploadedFileBytes = Page.Session["UploadedFile"];

        if (uploadedFileBytes == null)
        {
            return;
        }
        MemoryStream stream = MemoryStreamHelper.FromBytes(uploadedFileBytes);

        Page.Session["UploadedFile"] = null;
        if (stream == null)
        {
            return;
        }
        stream.WriteTo(Page.Response.OutputStream);
        Page.Response.ContentType    = "application/pdf";
        Page.Response.HeaderEncoding = System.Text.Encoding.UTF8;
        Page.Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}.pdf", Path.GetFileNameWithoutExtension(fileName)));
        Page.Response.End();
    }
コード例 #8
0
        private static async Task <bool> CreateUpdateItem(object cacheKey, object cacheObject, string region, int expireCacheTime, CacheItemPolicy cacheItemPolicy, bool allowSliddingTime = false)
        {
            var cacheData = await MemoryStreamHelper.SerializeObject(cacheObject);

            var key        = MemoryUtilities.CombinedKey(cacheKey, region);
            var expireTime = DateTime.UtcNow.AddMinutes(expireCacheTime);
            var item       = new BaseModel
            {
                CacheKey          = key,
                Expires           = expireTime,
                CacheObject       = cacheData,
                AllowSliddingTime = allowSliddingTime
            };

            bool results;

            lock (Sync)
            {
                results = _cache.Add(key, item, cacheItemPolicy);
            }

            return(results);
        }
コード例 #9
0
        public override async Task <bool> Add(object cacheKey, object cacheObject, string region, int expirationInMinutes = 15)
        {
            if (!_isEnabled)
            {
                return(true);
            }

            var expireCacheTime = expirationInMinutes == 15 ? _cacheExpirationTime : expirationInMinutes;

            if (await GetItem(cacheKey, region) != null)
            {
                await Remove(cacheKey, region);
            }

            var expireTime = DateTime.UtcNow.AddMinutes(expireCacheTime);
            var item       = new CacheItem
            {
                CacheKey    = cacheKey.ToString(),
                Expires     = expireTime,
                CacheObject = await MemoryStreamHelper.SerializeObject(cacheObject)
            };

            return(await CreateUpdateItem(region, item));
        }
コード例 #10
0
        public async Task <AppConfigData> GetAppConfigData()
        {
            // In general, we should limit the calls to GetConfiguration API call to at least once every 15 seconds
            // In AppConstants the TimeToLiveExpiration is set to the initial DateTime of Program.cs execution plus AppConstants.TimeToLiveInSeconds (15 seconds)
            // This if condition makes sure that we only call the GetConfiguration API call if we have not exceeded the TTL expiration, or
            // if the ClientConfigurationVersion is set in our local cache in AppConstants - ClientConfigurationVersion is returned from the initial call to the GetConfiguration API (see below for more info)
            if (DateTime.UtcNow > AppConstants.TimeToLiveExpiration || String.IsNullOrEmpty(AppConstants.ClientConfigurationVersion))
            {
                Console.WriteLine("CALLED GetConfigurationAPI to get AppConfigData  \n");
                IAppConfigService appConfigService = new AppConfigService(_clientId);

                // get Amazon.AppConfig.Model.GetConfigurationResponse from GetConfiguration API Call
                GetConfigurationResponse getConfigurationResponse = await appConfigService.GetConfigurationResponse();


                // add ConfigurationVersion to AppConstants to AppConstants to be used in subsequent calls to GetConfugration API to avopid excess charges
                // https://docs.aws.amazon.com/appconfig/2019-10-09/APIReference/API_GetConfiguration.html#API_GetConfiguration_RequestSyntax
                AppConstants.ClientConfigurationVersion = getConfigurationResponse.ConfigurationVersion;


                // The GetConfiguration response includes a Content section (i.e., our getConfigurationResponse.Content) that shows the configuration data.
                // The Content section only appears if the system finds new or updated configuration data.
                // If the system doesn't find new or updated configuration data, then the Content section is not returned (Null).
                // https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-retrieving-the-configuration.html
                string decodedResponseData = getConfigurationResponse.Content.Length > 0 ? MemoryStreamHelper.DecodeMemoryStreamToString(getConfigurationResponse.Content) : String.Empty;


                // convert DecodedResponseData to our AppConfigData model which consists of:
                // bool boolEnableLimitResults
                // int intResultLimit
                AppConfigData appConfigData = this.ConvertDecodedResponseToAppConfigData(decodedResponseData);

                // add AppConfigData to our cache in AppConstants
                AppConstants.AppConfigData = appConfigData;

                return(AppConstants.AppConfigData);
            }
            else
            {
                Console.WriteLine("DID NOT call GetConfigurationAPI to get data.  Return AppConfigData from cached value in AppConstants.AppConfigData instead. \n");
                return(AppConstants.AppConfigData);
            }
        }