ASP. NET use memcached high cache instance (Entry level)

Source: Internet
Author: User
Tags log4net
Memcached is a high-performance distributed memory object Cache System for dynamic web applications to reduce database load. It caches data and objects in the memory to reduce the number of times the database is read, so as to provide dynamic, database-driven website speed. Memcached is widely known as a cache technology solution. Many web applications are using it now-including some well-known websites. If you do not know this yet, please read it before reading it.
Memcached is used in ASP. NET, there are many Article All of them are introduced. The following are my personal experience.
I. Preparation
You need the following software:
Vs. Net (05/08)
Sqlserver
Memcached server and client class libraries (open-source software, download it)
The client class library includes the following dll:
Memcached. clientlibrary. dll
Icsharpcode. sharpziplib. dll
Log4net. dll
2. Install the memcached Server
Copy memcached.exe to any directory, such as C:. On the command line, enter:
Memcached.exe-D install
Memcached will act as a Service resident system memory
3. Create an ASP. NET Project
Create an ASP. netweb project named mmcweb and add references to the client class libraries mentioned above.
Iv. Configuration
Memcached uses log4net, so we first configure log4net
Find the configsections node in Web. config and add the following content:
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>

Add the following content to the configsections node:
<Log4net>
<Appender name = "rollinglogfileappender" type = "log4net. appender. rollingfileappender">
<Param name = "file" value = "logfiles/"/>
<Param name = "appendtofile" value = "true"/>
<Param name = "maxsizerollbackups" value = "10"/>
<Param name = "staticlogfilename" value = "false"/>
<Param name = "datepattern" value = "yyyy-mm-dd&quot;.txt & quot;"/>
<Param name = "rollingstyle" value = "date"/>
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "% d {yyyy-mm-dd hh: mm: SS} [% thread] %-5 level % logger % NDC-% message % newline "/>
</Layout>
</Appender>
<Appender name = "leleappender" type = "log4net. appender. consoleappender">
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "% d {yyyy-mm-dd hh: mm: SS} [% thread] %-5 level % logger % NDC-% message % newline "/>
</Layout>
</Appender>
<Root>
<Level value = "all"/>
<Appender-ref = "rollinglogfileappender"/>
<Appender-ref = "leleappender"/>
</Root>
<Logger name = "memcached. clientlibrary">
<Level value = "Warn"/>

</Logger>
</Log4net>
Start debugging. If no configuration error prompt is displayed and logfiles is in the directory of the website, log4net configuration is successful. 5. initialize sockiopool
What is sockiopool? Sockiopool is a socket connection pool provided by the memcached client. Generally speaking, it is an object that exchanges data with the memcached server. Sockiopool in ApplicationProgramYou can initialize it once at startup. I put this job in the application_start method of Global. asax. CS.
Char [] separator = {','};
String [] serverlist = configurationmanager. receivettings ["memcached. serverlist"]. Split (separator );

// Initialize the pool for memcache servers
Try
{
Sockiopool pool = sockiopool. getinstance ();
Pool. setservers (serverlist );

Pool. initconnections = 3;
Pool. minconnections = 3;
Pool. maxconnections = 50;

Pool. socketconnecttimeout = 1000;
Pool. Fig = 3000;

Pool. maintenancesleep = 30;
Pool. Failover = true;

Pool. Nagle = false;
Pool. initialize ();
}
Catch (exception ERR)
{
// Here we can use log4net to record the error!
}

pay attention to ettings ["memcached. serverlist "] is in the web. config, so the web. the sub-node of the appsettings in config must have the following line

Start the debugging server. If no error log is recorded, the IO connection pool has been opened successfully.
6. Using memcached
the question is finally reached. However, before using memcached, we still need to prepare some data.
Create an object class people and add the serializable attribute !!!
Add a table to the database. The field corresponds to the object class and some test data is inserted. The design of the persistence layer and business layer is skipped. They are responsible for providing some data with customizable return types, such as ilist and dataset.
memcached is easy to use. For example, a group of people-type data is retrieved in the background and stored in an arraylist called peoplelist, which is frequently used,
memcachedclient MC = new memcachedclient ();
MC. enablecompression = true;
MC. set (key, peoplelist);
the preceding key is used to access this arraylist. Data in memcached is saved as key-value pairs.
once MC. if keyexists (key) is true, return MC is used. get (key) as arraylist to extract data. When deleting the data, use return MC. delete (key); and so on. You can think about it yourself.

The above is just a demonstration. In fact, data cache is a complex and tedious task, not only the backgroundCodeIt also requires the database policies and tuning for access to large data volumes.

Asp.net itself has the cache function, which is very powerful (I recommend runtime cached). You can specify the cache time or the active time callback. Why do you need memcached?
I have always thought that memcached is a function that does not provide cached for PHP.

 

Related Article

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.