How is HttpContext.Current.Cache saving objects in memory?

2.1k Views Asked by At

I am using HttpContext.Current.Cache to save objects into memory.

The code i have looks something like this:

public void Add(string key, object data, TimeSpan slidingExpirationTime)
{
    HttpContext.Current.Cache.Insert(key, data, null, System.Web.Caching.Cache.NoAbsoluteExpiration, slidingExpirationTime);
}

public T Get<T>(string key)
{
    T itemStored = (T)HttpContext.Current.Cache.Get(key);
    if (itemStored == null)
        itemStored = default(T);

    return itemStored;
}

This works very fast!

I am curios how it's saving the object into memory.

Is it saving the pointer value, or is it hashing the object, then saving it into memory, and when i request it it deserializes it back?

1

There are 1 best solutions below

1
On BEST ANSWER

The data, is a kind of object and from the inside function that insert the cache key, we see that is simple keep a reference to the object

internal CacheEntry(string key, object value, CacheDependency dependency, CacheItemRemovedCallback onRemovedHandler, DateTime utcAbsoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, bool isPublic) : base(key, isPublic)
{
    if (value == null)
    {
        throw new ArgumentNullException("value");
    }
    .... code ....
    this._value = value;
    .... code ....
}