Base solution for your next web application
Open Closed

Cache #3936


User avatar
0
haytham created

hello How to save/Set data in Cache?

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

6 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team
    public class MyAppService
    {
        public ICacheManager CacheManager { get; set; }
        public ICache Cache => CacheManager.GetCache("MyCache");
    
        public void Set(int value)
        {
            Cache.Set("key", value);
        }
    }
    

    See:

    • <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Caching">https://aspnetboilerplate.com/Pages/Documents/Caching</a>
    • MemoryCacheManager_Tests.cs
    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    haytham created

    Thank you but how can read ??

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Haytham,

    This document also contains an example of Get <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Caching">https://aspnetboilerplate.com/Pages/Documents/Caching</a>.

    _cacheManager
                    .GetCache("MyCache")
                    .Get(id.ToString(), () => GetFromDatabase(id))
    

    GetFromDatabase method is runned if requested item is not in cache.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    haytham created

    Is it possible to use the Cache to pass the data from Action to Action in the Controller or from Action to view instead of (TempData,ViewBag)in ASP.Net MVC ?? Or is it a bad choice

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    aaron created
    Support Team

    These serve different purposes: ViewBag (dynamic) and ViewData - valid for the current request. TempData - valid for a single redirect. Session - valid for a login session. Cache - valid for 60 minutes (sliding expiration) while your application runs (singleton) and shared globally (with other users).

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    haytham created

    Can use session ,tempdata , viewbag in Cloud /SAAS ??

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)