Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple C# ASP.NET Cache Implementation

I need to build up a List<object> and cache the list and be able to append to it. I also need to be able to blow it away easily and recreate it. What is a simple way to accomplish this?

like image 404
Brian David Berman Avatar asked Mar 16 '10 21:03

Brian David Berman


People also ask

What is simple C?

The SIMPLEC (Semi-Implicit Method for Pressure Linked Equations-Consistent) algorithm; a modified form of SIMPLE algorithm; is a commonly used numerical procedure in Computational Fluid Dynamics to solve the very famous Navier–Stokes equations. This algorithm was developed by Van Doormal and Raithby in 1984.

Is C easy or C++ easy?

C++ was designed to be easier to use and to allow programmers to make efficient use of computer resources. C++ also has some similarities with C, but there are some important differences. C++ is a good choice for experienced programmers who want to learn a new programming language.

Is C an easy language?

While C is one of the more difficult languages to learn, it's still an excellent first language pick up because almost all programming languages are implemented in it. This means that once you learn C, it'll be simple to learn more languages like C++ and C#.


2 Answers

Something like this perhaps?

using System;
using System.Collections.Generic;
using System.Web;

public class MyListCache
{
    private List<object> _MyList = null;
    public List<object> MyList {
        get {
            if (_MyList == null) {
                _MyList = (HttpContext.Current.Cache["MyList"] as List<object>);
                if (_MyList == null) {
                    _MyList = new List<object>();
                    HttpContext.Current.Cache.Insert("MyList", _MyList);
                }
            }
            return _MyList;
        }
        set {
            HttpContext.Current.Cache.Insert("MyList", _MyList);
        }
    }

    public void ClearList() {
        HttpContext.Current.Cache.Remove("MyList");
    }
}

As for how to use.....

// Get an instance
var listCache = new MyListCache();

// Add something
listCache.MyList.Add(someObject);

// Enumerate
foreach(var o in listCache.MyList) {
  Console.WriteLine(o.ToString());
}  

// Blow it away
listCache.ClearList();
like image 61
jessegavin Avatar answered Sep 17 '22 20:09

jessegavin


This Tutorial is what I found to be helpful

  • ASP.NET Caching Features

Here is a sample

List<object> list = new List<Object>();

Cache["ObjectList"] = list;                 // add
list = ( List<object>) Cache["ObjectList"]; // retrieve
Cache.Remove("ObjectList");                 // remove
like image 41
Asad Avatar answered Sep 17 '22 20:09

Asad