


Memcached introduction
Let’s introduce Memcached.
1. What is Memcached
Memcached is an open source, high-performance, distributed memory object cache system that accesses data in the form of key-value teams. Memcached is simple and Powerful, its simple design promotes rapid deployment, is easy to develop, and solves many problems faced by big data caching.
The official website is: http://memcached.org/. Currently, many well-known Internet applications use Memcached, such as Wikipedia, Flickr, Youtube, WordPress, etc.
2. Download MemCached under Windows platform, the address is:
http://code.jellycan.com/files/memcached-1.2.6-win32-bin.zip
The corresponding source code address is:
http://code.jellycan.com/files/memcached-1.2.6-win32-src.zip
Then, unzip it, You will see a memcached.exe file. Install it as shown below. It will be installed on the machine as a system service.
Then, select this service and right-click the mouse to start it. Serve.
Enter: telnet 127.0.0.1 11211 in the DOS interface to confirm whether the service is started correctly. If it is correct, it will be displayed as follows
The ERROR shown in the above picture is when I enter the characters casually and press The carriage return is displayed because you need to install the protocol specified by memcached for input, otherwise the error shown above will be displayed.
3. Memcached protocol and data access
The so-called protocol can be understood as the grammatical rules for its operation (data access). Common commands and parameters for accessing data are as follows:
set: save a record
key: key value of the record
flags: decimal int, identifies the client flag when storing the record, and will be returned when the record is taken out .
exptim: The expiration time of the data, 0 means no expiration, other values represent the effective number of milliseconds. After expiration, the client will not be able to obtain this record, and the expired records in memcached will be cleared or delete.
get: means to get the value corresponding to key from memcached. If there is no corresponding value, return the end flag END
append: means to add the input content to the value corresponding to key at the end
delete: delete the value corresponding to the key
For more protocols, please refer to: protocol.txt included in the memcached package
Specific examples such as:
Required Note: If the specified character length is 5 during set, and the input content exceeds this length, an error will be reported: CLIENT_ERROR bad data chunk
4, Write code to perform data access operations on memcached
Generally speaking, you can use the open source encapsulated memcached client to operate memcached. Of course, you can also write socket communication in the code according to the memcached protocol. Program implementation.
Memcached-Java-Client download page:
http://github.com/gwhalin/Memcached-Java-Client/downloads, then select download:
java_memcached -release_2.5.1.zip
You can see some written examples in the unzipped Test directory. You can check the data storage and withdrawal status by running com.danga.MemCached.test. TestMemcached, here The code is also posted:
package com.danga.MemCached.test; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; import org.apache.log4j.*; public class TestMemcached { public static void main(String[] args) { // memcached should be running on port 11211 but NOT on 11212 BasicConfigurator.configure(); //缓存服务器地址,多台服务器则以逗号隔开,11211为memcached使用的端口号 String[] servers = { “localhost:11211″ }; //得到一个链接池对象并进行一些初始化工作 SockIOPool pool = SockIOPool.getInstance(); pool.setServers( servers ); pool.setFailover( true ); pool.setInitConn( 10 ); pool.setMinConn( 5 ); pool.setMaxConn( 250 ); //pool.setMaintSleep( 30 ); pool.setNagle( false ); pool.setSocketTO( 3000 ); pool.setAliveCheck( true ); pool.initialize(); MemCachedClient mcc = new MemCachedClient(); // turn off most memcached client logging: //Logger.getLogger( MemCachedClient.class.getName() ).setLevel( com.schooner.MemCached.Logger. ); //以下是数据写入和取出操作例子 for ( int i = 0; i < 10; i++ ) { boolean success = mcc.set( “” + i, “Hello!” ); String result = (String)mcc.get( “” + i ); System.out.println( String.format( “set( %d ): %s”, i, success ) ); System.out.println( String.format( “get( %d ): %s”, i, result ) ); } System.out.println( “\n\t — sleeping –\n” ); try { Thread.sleep( 10000 ); } catch ( Exception ex ) { } for ( int i = 0; i < 10; i++ ) { boolean success = mcc.set( “” + i, “Hello!” ); String result = (String)mcc.get( “” + i ); System.out.println( String.format( “set( %d ): %s”, i, success ) ); System.out.println( String.format( “get( %d ): %s”, i, result ) ); } } }
MemCached's java client example
package com.danga.MemCached.test; import com.danga.MemCached.*; public class TestMemcached { public static void main(String[] args) { /*初始化SockIOPool,管理memcached的连接池*/ String[] servers = { "192.168.105.217:11211" }; SockIOPool pool = SockIOPool.getInstance(); pool.setServers(servers); pool.setFailover(true); pool.setInitConn(10); pool.setMinConn(5); pool.setMaxConn(250); pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(3000); pool.setAliveCheck(true); pool.initialize(); /*建立MemcachedClient实例*/ MemCachedClient memCachedClient = new MemCachedClient(); for (int i = 0; i < 10; i++) { /*将对象加入到memcached缓存*/ boolean success = memCachedClient.set("" + i, "Hello!"); /*从memcached缓存中按key值取对象*/ String result = (String) memCachedClient.get("" + i); System.out.println(String.format("set( %d ): %s", i, success)); System.out.println(String.format("get( %d ): %s", i, result)); } } }
1. Unzip (in this case, unzip to c:\memcached).
2. In the command line state, enter: c:\memcached\memcached.exe -d install. At this point, memcached has been installed as a windows service
3. Enter: c:\memcached\memcached.exe -d start at the command line to start the memcached service. Of course, you can also choose to start it in the windows service
For more examples to explain the use of the Java client of the distributed caching software Memcached, please pay attention to the PHP Chinese website for related articles!

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

This article explains Java's NIO API for non-blocking I/O, using Selectors and Channels to handle multiple connections efficiently with a single thread. It details the process, benefits (scalability, performance), and potential pitfalls (complexity,

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

This article details Java's socket API for network communication, covering client-server setup, data handling, and crucial considerations like resource management, error handling, and security. It also explores performance optimization techniques, i


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.