Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
What is In Memory DB or In Memory Storage using MVC Web application.
I have a requirement in MVC web application as i want to select few data from database and store the data IN Memory for faster access.
For Example in SQL server I have 3 Columns and i select 50 records from the Sql Server and i want to Store this data as In memory data in MVC to faster access.
I know we can use Session to store the data and display the data from sesstion.
I know we can also use No SQl like MONGODB to store and retrive the data for faster access.
My question is there any other concept in MVC for IN Memory data storage apart from SQL Server,Sesstion or NO SQL concept.
Posted
Comments
Nathan Minier 7-Jan-15 7:24am    
Sure, make a cache object. You can use the built-in one, or make your own and use it with static access. I prefer to make my own, since I've found the IIS cache to behave unexpectedly, depending on how the server is configured.
mak1990 10-Jan-15 0:23am    
Can you give me some example or referance website

1 solution

Here's some source for a baseline, generic cache that is similar to one that I use.

C#
using System;
using System.Collections.Generic;

namespace GenericCache
{
    public class SearchCache<T> : List<T> 
        where T : class
    {
        public delegate IEnumerable<T> DataSourceFunction();

        #region Properties
        // Instance control
        private static SearchCache<T> _instance;
        private static readonly object _syncLock = new object();
        

        // Cache refresh control
        public DateTime Initialized { get; private set; }
        public DateTime Refreshed { get; private set; }
        public int Interval { get; private set; }

        public DataSourceFunction DataSource { get; set; }

        // Hit tracking to for usage and efficiency statistics
        public int Hits { get; protected set; }
        public int ReachBacks { get; protected set; }

        public int CacheHits
        {
            get { return Hits - ReachBacks; }
        }

        #endregion

        #region CTOR/DTOR
        public SearchCache(DataSourceFunction dataSource)
        {
            Initialized = DateTime.Now;
            Interval = 60; // refresh time in minutes
            Hits = 0;
            ReachBacks = 0;
            DataSource = dataSource;
            CheckInterval();
        }

        #endregion

        #region Internal Methods

        /// <summary>
        /// Make sure that the singleton is only populated per interval.
        /// </summary>
        private void CheckInterval()
        {
                Hits++;

                // If the update threshold has been exceeded.
                if (Refreshed.AddMinutes(Interval) < DateTime.Now)
                {
                    Populate();
                }            
        }

        /// <summary>
        /// Get search data into the cache
        /// </summary>
        private void Populate()
        {

                // Empty Cache
                this.Clear();                

                this.AddRange(DataSource());

                // Statistical tracking
                ReachBacks++;
                Refreshed = DateTime.Now;
            
        }
        #endregion

        #region External Methods
        public static SearchCache<T> Instance(DataSourceFunction dataSource)
        {

                if (_instance == null)
                {
                    lock (_syncLock)
                    {
                        _instance = new SearchCache<T>(dataSource);
                    }
                }
                _instance.CheckInterval();
                return _instance;
            
        }

        /// <summary>
        /// Forces the cache to refresh outside the normal interval
        /// </summary> 
        public void Refresh()
        {
            Hits++;
            Populate();
        }

        /// <summary>
        ///  Reset the cache to an initialized state
        /// </summary>
        public void Zeroize()
        {
            Clear();
            Initialized = DateTime.Now;

            Hits = 1;
            ReachBacks = 0;

            Populate();
        }
        #endregion
    }
}


You need a data source function that provides IEnumerable<T>, and it is populated with:

C#
var myCache = SearchCache<T>.Instance(/*function that return IEnumerable<T>*/);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900