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/EnvironmentVersion/DescriptionVerified
Memcached1.6.39Yes
libeventSystem repo defaultYes
EVCache Client4.139.0Yes
spymemcached2.12.3Yes
Eureka Client1.5.6Yes

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:

  1. In-memory Storage: All data stored in memory, extremely fast read/write
  2. Key-value Storage: Uses simple key-value pair storage structure
  3. Distributed Architecture: Supports multi-server cluster deployment
  4. LRU Algorithm: When memory is insufficient, automatically clears data using least recently used algorithm

Installation Steps

  1. Install dependencies
yum install libevent libevent-devel gcc-c++
  1. 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
  1. Startup parameter explanation
ParameterDescription
-dStart in daemon mode
-mMemory size (MB)
-uRun user
-lListen address
-pPort (default 11211)
-cMax concurrent connections
-PPID 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

SymptomRoot CauseFix
EVCache get returns nullKey expiration too short or spelling inconsistentExtend expiration time, confirm key naming
Connection refused-l listen address inconsistent with clientUse 0.0.0.0 or correct IP
Permission deniedpid file directory no write permissionAdjust directory permissions