Sunday, March 25, 2018

ASP.Net core 2 caching

With classic Asp.Net you used the HtppContext in the System.Web namespace for caching. Since Asp.Net Core was created to be cross platform caching is done differently.

I am using Visual Studio for Mac to do this but it will work the same in Visual Studio 2017 (for windows).

Lets create a new Asp.Net core application





Once the app is created go to the startup.cs class. and change the ConfigureServices method to this

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddMemoryCache();
        }
  


Now to use this we need to get access to the IMemoryCache in the controllers constructor.  In the demo I will use the about page in the project created

        IMemoryCache caching= null;


        public HomeController(IMemoryCache cache)
        {
            caching = cache;
        }

To use it. I am just going to cache the DateTime

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";
            DateTime currentTime;
            if (!caching.TryGetValue<DateTime>("currentTime", out currentTime))
            {
                currentTime = DateTime.Now;
                caching.Set<DateTime>("currentTime", currentTime, DateTimeOffset.Now.AddMinutes(10));
            }
        
                
            ViewBag.Time = currentTime;

            return View();
        }

To cache something use the Set method.  I would recommend using the version that allow you to specify the type of object.

When setting the cache you pass in the key, what you want cached and optionally how long you want it cached.  I set it to 10 minutes in this example.

Getting items from the cache you use TryGetValue it will return true if the value was found and it passes it out in an out parameter.

If you need to remove something use the cache Remove method where you pass in the key of the object you want to remove.

To show the item is cached I changed the About.cshtml to show the current date time and the cached value

@{
    ViewData["Title"] = "About";
}
<h2>@ViewData["Title"]</h2>
<h3>@ViewData["Message"]</h3>

<p>Use this area to provide additional information.</p>
@DateTime.Now
<br/>
@ViewBag.Time





Please be sure to check out my course on Packt about asp.net core to learn more about asp.net core










Mac for windows users

I recently bought a Mac to start developing iPhone apps

Here are some of the things I found that are different in Mac than a Windows PC

Copy is Command + c
Paste is Command + v
Undo is Command + z

You can use Command + Shift + 4 to take a screen shot like with the snippet tool in windows.  The images saves to the desktop

The delete key works like backspace on the mac.  Command + delete acts like the windows delete key


Make sure in Finder that the c drive is selected if you want finder to work more like windows explorer



Orlando Code Camp 2018

At the 2018 Orlando Code Camp I did a presentation on using Prism with Xamarin Forms.

Here is a link to the sample code and slides

https://github.com/vb2ae/orlandocodecampprism


The slides are in the GitHub repository.  They are in both KeyNote and PowerPoint format