Custom cache for memcache and Cache

Source: Internet
Author: User
Tags delete cache

* ***** Cache storage configuration items (for direct nodes with read cache and appsetting )*****

Memcache is often used for data that does not need to be changed frequently and does not need to be read from the database every time. It can prevent the cache from reading quickly in the memory, however, memcache (a distributed cache) does not have a storage mechanism. If the server fails, memcache is lost.

 

The system configuration section is placed in the appsettings configuration section <add key = "mingzi" value = "Assas"/>

Configurationmanager. deleetting ["mingzi"]; get

 

Cachehelper

Public class cachehelper

{

/// <Summary>

/// Obtain the cache

/// </Summary>

/// <Param name = "key"> </param>

/// <Param name = "value"> </param>

Public static void set (string key, string value)

{

System. Web. caching. cache = httpruntime. cache;

Cache [Key] = value;

}

/// <Summary>

/// Set Cache

/// </Summary>

/// <Param name = "key"> </param>

/// <Returns> </returns>

Public static object get (string key)

{

System. Web. caching. cache = httpruntime. cache;

Return cache [Key];

}

/// <Summary>

/// Remove the cache

/// </Summary>

/// <Param name = "key"> </param>

Public static void remove (string key)

{

System. Web. caching. cache = httpruntime. cache;

Cache. Remove (key );

}

}

 

Memcachehelper

Private Static readonly memcachedclient MC = NULL;

Static memcachehelper ()

{

String [] serverlist = {"127.0.0.1: 11211"}; // define the IP address and port number in webconfig

// Initialize the pool

Sockiopool pool = sockiopool. getinstance ();

Pool. setservers (serverlist );

Pool. initconnections = 3;

Pool. minconnections = 3;

Pool. maxconnections = 5;

Pool. socketconnecttimeout = 1000;

Pool. Fig = 3000;

Pool. maintenancesleep = 30;

Pool. Failover = true;

Pool. Nagle = false;

Pool. initialize ();

// Obtain the client instance

MC = new memcachedclient ();

MC. enablecompression = false;

}

/// <Summary>

/// Set

/// </Summary>

/// <Param name = "key"> </param>

/// <Param name = "value"> </param>

Public static void set (string key, object value)

{

MC. Set (Key, value );

}

/// <Summary>

/// Obtain the cache

/// </Summary>

/// <Param name = "key"> </param>

/// <Returns> </returns>

Public static object get (string key)

{

Return MC. Get (key );

}

/// <Summary>

/// Set the cache and set the expiration time

/// </Summary>

/// <Param name = "key"> </param>

/// <Param name = "value"> </param>

/// <Param name = "DT"> </param>

Public static void set (string key, object value, datetime DT)

{

MC. Set (Key, value, DT );

}

/// <Summary>

/// Delete Cache

/// </Summary>

/// <Param name = "key"> </param>

/// <Returns> </returns>

Public static bool Delete (string key)

{

If (MC. keyexists (key ))

{

Return MC. Delete (key );

}

Return false;

}

}

 

 

// When reading, first judge that if this configuration section is included in the cache data, the system will directly return the data read from the database and read from the database if this configuration section is not available. put in cache

Public String getvalue (string key)

{

// Add cache dependencies. Do not forget to remove the cache when updating data.

// Object OBJ = Common. cachehelper. Get ("setting _" + key); // for the first time, the object is not retrieved from the database.

// If (OBJ = NULL)

//{

// Var setting = This. dbsession. settingsdal. loadentities (C => C. Name = key). firstordefault ();

// If (setting! = NULL)

//{

// Common. cachehelper. Set ("setting _" + key, setting. value );

//// Read the data and put it into the cache

//// Return the corresponding value

// Return setting. value;

//}

//}

// Return obj. tostring ();

 

Object o = bookshop. Common. memcachehelper. Get ("setting _" + key );

If (O = NULL)

{

VaR setting = This. dbsession. settingsdal. loadentities (C => C. Name = key). firstordefault ();

If (setting! = NULL)

{

Bookshop. Common. memcachehelper. Set ("setting _" + key, setting. value );

Return setting. value;

}

}

Return O. tostring ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.