locked
MemoryCache Namespace instead of Class RRS feed

  • Question

  • User-579260936 posted

    I want to use System.Runtime.Caching.MemoryCache. But if I import it and use MemoryCache, there is a namespace called MemoryCache and I cannot get the class MemoryCache.

    I can get the class with System.Runtime.Caching.MemoryCache, but writing that everytime is not how it should be done. And is very annoying. Why can't I get the Class? And where is the Namespace coming from?

    Thursday, October 19, 2017 8:07 PM

All replies

  • User347430248 posted

    Hi Sakkaku,

    did you try to assign it to object when you include the namespace?

    like below.

    using nmspce=System.Runtime.Caching.MemoryCache;

    you can try to use like it and then every time you not need to write whole namespace. instead of that you can just use "nmspce" (it is just object name that I use here. you can name it whatever you like.)

    below is one example for  System.Runtime.Caching.MemoryCache.

    private void btnGet_Click(object sender, EventArgs e)
    {
        ObjectCache cache = MemoryCache.Default;
        string fileContents = cache["filecontents"] as string;
    
        if (fileContents == null)
        {
            CacheItemPolicy policy = new CacheItemPolicy();
    
            List<string> filePaths = new List<string>();
            filePaths.Add("c:\\cache\\example.txt");
    
            policy.ChangeMonitors.Add(new 
            HostFileChangeMonitor(filePaths));
    
            // Fetch the file contents.
            fileContents = 
                File.ReadAllText("c:\\cache\\example.txt");
    
            cache.Set("filecontents", fileContents, policy);
        }
    
        Label1.Text = fileContents;
    }

    Reference:

    MemoryCache Class

    How to: Cache Objects Simply using System.Runtime.Caching.MemoryCache

    Using MemoryCache in .NET 4.0

    Working with System.Runtime.Caching.MemoryCache

    Regards

    Deepak

    Friday, October 20, 2017 3:14 AM