In the previous article, we talked about why to use memched as a cache server (please click here for students who don't see). Let's make a "Hello World" program based on the MEMCACHED-1.2.1-WIN32 version of the service component (after installation is done daemon with a Windows service) and C#api (enyim.caching). Let us really feel that memcached is right beside us. In the latter article, we also talk about the core parts of memcached (hash access data based on key, cache data on the server side of the memory storage structure) and some good cases.
The following example implements the function very simply, according to the key to access an object (to support serializable), because the server-side data is a byte-type data group implementation exists.
Start of service:
1, the Memcached-1.2.1-win32.zip will be resolved to the specified place, such as c:\memcached
2, command line input ' c:\memcached\memcached.exe-d install '
3, command line input ' c:\memcached\memcached.exe-d start ', the command starts memcached, the default listener port is 11211
You can view its help through memcached.exe-h
First step: Configure config file
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<configSections>
<sectiongroup name= "enyim.com" >
<section name= "memcached" type= "Enyim.Caching.Configuration.MemcachedClientSection, enyim.caching"/>
</sectionGroup>
<section name= "memcached" type= "Enyim.Caching.Configuration.MemcachedClientSection, enyim.caching"/>
</configSections>
<enyim.com>
<memcached>
<servers>
<!--put your own server (s) here-->
<add address= "127.0.0.1" port= "11211"/>
</servers>
<socketpool minpoolsize= "Ten" maxpoolsize= "connectiontimeout=" 00:00:10 "deadtimeout=" 00:02:00 "/>"
</memcached>
</enyim.com>
<memcached keytransformer= "Enyim.Caching.TigerHashTransformer, enyim.caching" >
<servers>
<add address= "127.0.0.1" port= "11211"/>
</servers>
<socketpool minpoolsize= "2" maxpoolsize= "connectiontimeout=" 00:00:10 "deadtimeout=" 00:02:00 "/>
</memcached>
</configuration>
The port:11211 here is the port that Memcached-1.2.1-win32 uses by default when installing. Of course you can use the Memcached.exe-p port number to come from the line settings.
The second step, the new Testmemcachedapp console project
Refer to Enyim.Caching.dll or join this project in solution (found in the code that can be downloaded).
The base code is as follows:
Create a instance of Memcachedclient
memcachedclient mc = new Memcachedclient ();
Store a string in the cache
Mc. Store (Storemode.set, "MyKey", "Hello World");
Retrieve the item from the cache
Console.WriteLine (MC. Get ("MyKey"));
The complete code is as follows,
Using System;
Using System.Collections.Generic;
Using System.Text;
Using Enyim.caching;
Using Enyim.Caching.Memcached;
Using System.Net;
Using Enyim.Caching.Configuration;
Namespace DemoApp
{
Class Program
{
static void Main (string[] args)
{
Create a Memcachedclient
In your application your can cache the client in a static variable or just recreate it every time
memcachedclient mc = new Memcachedclient ();
Store a string in the cache
Mc. Store (Storemode.set, "MyKey", "Hello World");
Retrieve the item from the cache
Console.WriteLine (MC. Get ("MyKey"));
Store some other items
Mc. Store (Storemode.set, "D1", 1234L);
Mc. Store (Storemode.set, "D2", DateTime.Now);
Mc. Store (Storemode.set, "D3", true);
Mc. Store (Storemode.set, "D4", New Product ());
Mc. Store (Storemode.set, "D5", new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
Console.WriteLine ("D1: {0}", MC. Get ("D1"));
Console.WriteLine ("D2: {0}", MC. Get ("D2"));
Console.WriteLine ("D3: {0}", MC. Get ("D3"));
Console.WriteLine ("D4: {0}", MC. Get ("D4"));
byte[] tmp = MC. Get<byte[]> ("D5");
Delete them from the cache
Mc. Remove ("D1");
Mc. Remove ("D2");
Mc. Remove ("D3");
Mc. Remove ("D4");
Add an item which are valid for ten mins
Mc. Store (Storemode.set, "D4", New Product (), new TimeSpan (0, 10, 0));
Console.ReadLine ();
}
Objects must be serializable to being able to store them in the cache
[Serializable]
Class Product
{
public double price = 1.24;
public string Name = "Mineral water";
public override string ToString ()
{
Return String.Format ("Product {{0}: {1}}}", this. Name, this. Price);
}
}
}
}
Server and Client API and instance code download (modified on Enyim Memcached 1.2. Version 0.2)
Download memcached Service installation Address: http://www.danga.com/memcached/
Client api:http://www.danga.com/memcached/apis.bml
Enyim.com Memcached client:memcached Instance