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 and many articles have been introduced. The following is 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".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 for exchanging data with the memcached server. Sockiopool can be initialized once when the application is started. 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! } Note that the appsettings ["memcached. serverlist"] is set in Web. config. Therefore, the following line must be set in the child node of the appsettings in Web. config. <Add key = "memcached. serverlist" value = "Maid: 11211"/> Start the debugging server. If no error log is recorded, the I/O connection pool is successfully opened. 6. Use memcached Finally, we entered the topic, but before using it, we still need to prepare some data. Create an object class people and add the serializable attribute !!! Add a table to the corresponding database. The field corresponds to the entity 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. This arraylist must be used frequently. Memcachedclient MC = new memcachedclient (); MC. enablecompression = true; MC. Set (Key, peoplelist ); The above key is used to access this arraylist key, and the data in memcached is saved as a key-value pair. Once MC. keyexists (key) is true, return MC. Get (key) as arraylist is used to extract data. When deleting the data, return MC. Delete (key); is used. You can think about it yourself. The above is just a demonstration. In fact, data cache is a complex and tedious task. It requires not only hierarchical optimization of the background code, but also database policies and tuning for access to large data volumes. Download memcached in windowMemcached-win32Installation: After the memcahcedfile is downloaded to the C drive, a memcached.exe file is displayed in the directory. Double-click it to start memcached.exe, open the window, or enable c: \ memcached \ memcached.exe-d start in cmd. After the memcahced server is started, you can see in the System Service that the memcahced Server service is running. The installation is OK. |