Configure and use memcached

Source: Internet
Author: User
Tags failover

1. Configuration

<! -- Memcached configuration -->
<Add key = "MemcachedServerlist" value = "Wagner. 0.0.1: 11211"/>
<Add key = "InitConnections" value = "3"/>
<Add key = "MinConnections" value = "3"/>
<Add key = "MaxConnections" value = "5"/>
<Add key = "SocketConnectTimeout" value = "1000"/>
<Add key = "SocketTimeout" value = "3000"/>
<Add key = "MaintenanceSleep" value = "30"/>
<Add key = "Failover" value = "true"/>
<Add key = "Nagle" value = "false"/>
<Add key = "CacheTimeout" value = "10"/>

 

2. memcached management class

 

/*
* Memcache management class
* Author: domo
* Date: 2012-04-17
*/

# Region reference

Using System;
Using System. Configuration;
Using Memcached. ClientLibrary;

# Endregion

Namespace bases. cache
{
/// <Summary>
/// Memcache management class
/// </Summary>
Public class MemcacheManager
{
/// <Summary>
/// Define the Memcache Client
/// </Summary>
Internal MemcachedClient Mc = new MemcachedClient {EnableCompression = false };

/// <Summary>
/// Construct initialization parameters
/// </Summary>
Public MemcacheManager ()
{
Var serverlist = ConfigurationManager. receivettings ["MemcachedServerlist"]. Split (',');
Var initConnections = ConfigurationManager. deleettings ["InitConnections"];
Var minConnections = ConfigurationManager. receivettings ["MinConnections"];
Var maxConnections = ConfigurationManager. etettings ["MaxConnections"];
Var socketConnectTimeout = ConfigurationManager. etetout ["SocketConnectTimeout"];
Var socketTimeout = ConfigurationManager. etettimeout ["SocketTimeout"];
Var maintenanceSleep = ConfigurationManager. receivettings ["MaintenanceSleep"];
Var failover = ConfigurationManager. receivettings ["Failover"];
Var nagle = ConfigurationManager. receivettings ["Nagle"];

Var pool = SockIOPool. GetInstance ();

Pool. SetServers (serverlist );

Pool. InitConnections = Convert. ToInt32 (initConnections );
Pool. MinConnections = Convert. ToInt32 (minConnections );
Pool. MaxConnections = Convert. ToInt32 (maxConnections );

Pool. SocketConnectTimeout = Convert. ToInt32 (socketConnectTimeout );
Pool. SocketTimeout = Convert. ToInt32 (socketTimeout );

Pool. MaintenanceSleep = Convert. ToInt64 (maintenanceSleep );
Pool. Failover = Convert. ToBoolean (failover );

Pool. Nagle = Convert. ToBoolean (nagle );
Pool. Initialize ();
}

/// <Summary>
/// Add cache
/// </Summary>
/// <Param name = "key"> </param>
/// <Param name = "value"> </param>
Public void Add (string key, object value)
{
Var cacheTimeout = ConfigurationManager. deleettimeout ["CacheTimeout"];

If (! Mc. KeyExists (key ))
Mc. Set (key, value, DateTime. Now. AddMinutes (Convert. ToDouble (cacheTimeout )));
}

/// <Summary>
/// Remove a cache
/// </Summary>
/// <Param name = "key"> </param>
Public void Delete (string key)
{
Mc. Delete (key );
}

/// <Summary>
/// Obtain the value of a cache
/// </Summary>
/// <Param name = "key"> </param>
/// <Returns> </returns>
Public object GetValue (string key)
{
Return Mc. KeyExists (key )? Mc. Get (key): null;
}

/// <Summary>
/// Clear all the caches
/// </Summary>
Public void Flush ()
{
Mc. FlushAll ();
}

/// <Summary>
/// Disable sockets
/// </Summary>
Public void Shutdown ()
{
SockIOPool. GetInstance (). Shutdown ();
}
}
}

3. Use

 

MemcacheManager memcacheManager = new MemcacheManager ();

If (memcacheManager. GetValue ("test") = null)
MemcacheManager. Add ("test", "my value ");

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.