TL;DR
- Scenario: Java applications wanting to experience Netflix EVCache locally or in small teams, but server not open source, can only self-build based on Memcached
- Conclusion: Compile Memcached 1.6.39 from source, start according to EVCache node configuration specs, can complete basic read/write and链路 verification
- Output: End-to-end configuration guide from compiling Memcached, startup parameter explanation, to EVCache Client POM dependency and example code
Version Matrix
| Component/Environment | Version/Description | Verified |
|---|---|---|
| Memcached | 1.6.39 | Yes |
| libevent | System repo default | Yes |
| EVCache Client | 4.139.0 | Yes |
| spymemcached | 2.12.3 | Yes |
| Eureka Client | 1.5.6 | Yes |
Install Memcached
Introduction
Memcached is an open-source, high-performance distributed in-memory object caching system, mainly used to reduce database load and improve access speed of dynamic web applications.
Core Features:
- In-memory Storage: All data stored in memory, extremely fast read/write
- Key-value Storage: Uses simple key-value pair storage structure
- Distributed Architecture: Supports multi-server cluster deployment
- LRU Algorithm: When memory is insufficient, automatically clears data using least recently used algorithm
Installation Steps
- Install dependencies
yum install libevent libevent-devel gcc-c++
- Download and compile
cd /opt/software
wget http://memcached.org/latest
tar -zxvf latest
cd memcached-1.6.39
./configure --prefix=/usr/memcached
make
make install
- Startup parameter explanation
| Parameter | Description |
|---|---|
| -d | Start in daemon mode |
| -m | Memory size (MB) |
| -u | Run user |
| -l | Listen address |
| -p | Port (default 11211) |
| -c | Max concurrent connections |
| -P | PID file path |
Startup example:
./memcached -d -m 1000 -u root -l 0.0.0.0 -p 11211 -c 256 -P /tmp/memcached.pid
Using EVCache Client
POM Dependencies
<dependencies>
<dependency>
<groupId>com.netflix.evcache</groupId>
<artifactId>evcache-client</artifactId>
<version>4.139.0</version>
</dependency>
<dependency>
<groupId>net.spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.12.3</version>
</dependency>
</dependencies>
Example Code
package icu.wzk;
import com.netflix.evcache.EVCache;
public class CacheDemo01 {
public static void main(String[] args) throws Exception {
String server = "SERVERGROUP1=172.16.1.130:11211";
System.setProperty("EVCACHE_1.use.simple.node.list.provider", "true");
System.setProperty("EVCACHE_1-NODES", server);
EVCache evCache = new EVCache.Builder()
.setAppName("EVCACHE_1")
.build();
evCache.set("wzk", "icu", 10);
String data = evCache.get("wzk");
System.out.println(data);
}
}
Error Quick Reference
| Symptom | Root Cause | Fix |
|---|---|---|
| EVCache get returns null | Key expiration too short or spelling inconsistent | Extend expiration time, confirm key naming |
| Connection refused | -l listen address inconsistent with client | Use 0.0.0.0 or correct IP |
| Permission denied | pid file directory no write permission | Adjust directory permissions |