locked
Cache (Add data, retrieve data and remove data from cache) in web services page RRS feed

  • Question

  • User-665585339 posted

    Hi,

    Is anyone have a sample code or tutorial on web services page (.asmx), to add data to cache, retrieve existing data from cache, or remove data from cache?

    Sunday, January 19, 2014 10:41 PM

Answers

  • User-1509636757 posted

    can i find the cache in my computer

    Cache stored in web server memery.

    i required to check, if someone pass in Value3, i needed to check select from cache whether Value3 existed.

            //-- check if Cache variable with keyName "Value3" exists or not
            if (Cache["Value3"] != null)
            {
                //-- your code
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 20, 2014 3:01 AM

All replies

  • User-1509636757 posted

    You can always access the Cache through the static HttpRuntime.Cache property, even if you don't have an HttpContext at the moment. you can use caching in web services. You have to choose the correct cache implementation based on service implementation. For example, if you are using ASP.NET web services then you can probably use ASP.NET Cache - available via HttpContext.Current.Cache.

    To insert/add data (data of any inbuilt or custom type like list of customer object for example): HttpContext.Current.Cache.Insert("CacheKey","ValueToInsert");

    To remove data from Cache:

    HttpContext.Current.Cache.Remove("CacheKey");

    If you explore more about Insert and Remove method of Cache, you will be able to get more idea about different 5 overloading method that Insert method provides, like you can add expiration as:

    HttpContext.Current.Cache.Insert("CacheKey", valueToStore, null, DateTime.Now.AddMinutes(60), Cache.NoSlidingExpiration);

    hope it helps./.

    Sunday, January 19, 2014 11:51 PM
  • User-665585339 posted

    Many thanks dude...

    Then let say after i insert data as below:-

    HttpContext.Current.Cache.Insert("CacheKey_storage","Value1");

    Then i close the application, can i retrieve it in somewhere in server?

    Basically, i needed a cache to store referenceID, and application will check whether the ID existed in cache, if existed, then reject, to prevent duplicate ID proceed further.

    I might needed to store the referenceID in cache for more than one month++.

    Monday, January 20, 2014 1:00 AM
  • User-1509636757 posted

    Then i close the application, can i retrieve it in somewhere in server?

    Yes, you can.. untill the expiration that you have set. But, you need to check for nulls always before accessing elsewhere in your application. For more than month you can add expiration like:

    HttpContext.Current.Cache.Insert("CacheKey", valueToStore, null, DateTime.Now.AddMonth(2), Cache.NoSlidingExpiration); //-- to add two months to expire.

    hope it helps./.

    Monday, January 20, 2014 1:15 AM
  • User-665585339 posted

    So, can i do like this?

    add

    -Value1

    -Value2

    -Value3

     

    Let say i have 3 values in my cache named (Temp_Cache), physically can i find the cache in my computer (Not via application cache retrieve).

    Actually, during runtime, i required to check, if someone pass in Value3, i needed to check select from cache whether Value3 existed.

    But the Cache only have Insert and Remove

    Monday, January 20, 2014 1:29 AM
  • User-1509636757 posted

    can i find the cache in my computer

    Cache stored in web server memery.

    i required to check, if someone pass in Value3, i needed to check select from cache whether Value3 existed.

            //-- check if Cache variable with keyName "Value3" exists or not
            if (Cache["Value3"] != null)
            {
                //-- your code
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 20, 2014 3:01 AM