什么是MVC中的内存数据库 [英] What is In Memory DB in MVC

查看:52
本文介绍了什么是MVC中的内存数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

什么是使用MVC Web应用程序的内存数据库或内存存储器。

我对MVC Web应用程序有一个要求,因为我想选择一些来自数据库的数据并将数据存储在内存中以便更快地访问。

例如在SQL服务器中我有3列,我从Sql Server中选择50条记录,我想将此数据存储为内存数据在MVC中更快访问。

我知道我们可以使用Session来存储数据并显示来自sesstion的数据。

我知道我们也可以像MONGODB一样使用No SQl存储和检索数据以便更快地访问。

我的问题是除了SQL Server,Sesstion或NO SQL概念之外,MVC中还有IN内存数据存储的任何其他概念。

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.

推荐答案

以下是基线的一些来源,通用缓存与我使用的类似。



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

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
    }
}





你需要一个提供IEnumerable< T>的数据源函数,它包含:





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

var myCache = SearchCache<T>.Instance(/*function that return IEnumerable<T>*/);


这篇关于什么是MVC中的内存数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆