Memcached Series 1: memcached instances

Source: Internet
Author: User

Http://www.cnblogs.com/luyinghuai/archive/2008/08/28/1278200.html reprinted

In the previous article, we explained why memched should be used as a cache server (Click here if you have not read it ). Next let's take the memcached-1.2.1-win32 version of the service components (installed with a windows service daemon) and C # API (Enyim. caching) as the basis, to build a "Hello world" program, let us really feel that memcached is around us. In the next article, we will also talk about the core part of memcached (using the key to hash Data Access and cache the memory storage structure of data on the server) and some good cases.

The following example provides a simple function. You can use the key to access an object (only Serializable is supported), because the server-side data is implemented by byte data groups.

Service startup:

1. Resolve memcached-1.2.1-win32.zip to a specified location, such as c: \ memcached.

2. Enter 'C: \ memcached \ memcached.exe-d install' in the command line'
3. Enter 'C: \ memcached \ memcached.exe-d start' in the command line to start Memcached. The default listening port is 11211.
You can use memcached.exe-h to view its help information.

Step 1: configure the 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 = "10" maxPoolSize = "100" 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 = "100" connectionTimeout = "00:00:10" deadTimeout = "00:02:00"/>
</Memcached>
</Configuration>

Here, port: 11211 is the port.you can use memcached-1.2.1-win32at the time of installation. Of course, you can use the memcached.exe-p port from the row.

Step 2: Create the console project of TestMemcachedApp

Reference Enyim. Caching. dll or add this project to solution (which can be found in the downloaded code ).

The basic 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 you 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 is valid for 10 mins
Mc. Store (StoreMode. Set, "D4", new Product (), new TimeSpan (0, 10, 0 ));
Console. ReadLine ();
}
// Objects must be serializable to be able to store them in the cache
[Serializable]
Class Product
{
Public double prices = 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 (Modification on Enyim Memcached 1.2.0.2)

Download memcached service installation address: http://www.danga.com/memcached/

Client API: http://www.danga.com/memcached/apis.bml

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.