Skip to content

Commit f4a1c11

Browse files
committed
Merge branch 'master' of github.com:Netflix/EVCache
2 parents 0bb749b + bac5b0d commit f4a1c11

File tree

1 file changed

+91
-35
lines changed

1 file changed

+91
-35
lines changed

evcache-client-sample/src/main/java/com/netflix/evcache/sample/EVCacheClientSample.java

+91-35
Original file line numberDiff line numberDiff line change
@@ -7,86 +7,142 @@
77

88
/**
99
* Created by senugula on 3/24/16.
10+
* Updated by akpratt on 5/13/16.
1011
*/
1112

1213
/**
13-
* This standalone program demonstrates how to use EVCacheClient for set/get operations using memcached running local box.
14-
* Prerequisite : ( Install memcached locally, most mac OS already have memcached installed.
15-
* Start memcached on local machine.
16-
* SERVERGROUP1:
17-
* memcached -d -p 11211
18-
* SERVERGROUP2:
19-
* memcached -d -p 11212
14+
* This standalone program demonstrates how to use EVCacheClient for
15+
* set/get operations using memcached running on your local box.
2016
*
17+
* By default, this program expects there to be two memcached processes
18+
* on the local host, on ports 11211 and 11212. They get used as two
19+
* replicas of a single shard each.
20+
*
21+
* You can override this configuration by setting the environment
22+
* variable EVC_SAMPLE_DEPLOYMENT to a string which describes your
23+
* deployment. The format for that string is as described in the EVCache
24+
* documentation for a simple node list provider. It would look like
25+
* this for a two-replica deployment with two shards per replica:
26+
*
27+
* SERVERGROUP1=host1:port1,host2:port2;SERVERGROUP2=host3:port3,host4:port4
2128
*/
29+
2230
public class EVCacheClientSample {
2331

2432
private final EVCache evCache;
25-
private final String key = "evcacheSample";
26-
private final String value = " This is my value1";
27-
private final int timeToLive = 100;
33+
private static boolean verboseMode = false;
2834

35+
/**
36+
* Default constructor.
37+
*
38+
* This tells the EVCache library to use the "simple node list
39+
* provider" for EVCACHE_APP1 (by setting the relevant system
40+
* property), and then it copies the EVC_SAMPLE_DEPLOYMENT
41+
* environment variable to the EVCACHE_APP1-NODES system property.
42+
*
43+
* If the environment variable isn't set, default is two shards on
44+
* localhost, on port 11211 and 11212, configured as two replicas with
45+
* one shard each.
46+
*
47+
* Finally, this initializes "evCache" using EVCache.Builder,
48+
* specifying the application name "EVCACHE_APP1."
49+
*/
2950
public EVCacheClientSample() {
30-
31-
/**
32-
* Enable node list provider
33-
* EVCACHE_APP1.use.simple.node.list.provider"
34-
*
35-
* Setup 2 server groups which is two memcached processes/nodes running at different ports acting different
36-
* server groups/auto-scaling groups for application EVCACHE_APP1
37-
* EVCACHE_APP1-NODES=SERVERGROUP1=localhost:11211;SERVERGROUP2=localhost:11212
38-
*/
51+
String deploymentDescriptor = System.getenv("EVC_SAMPLE_DEPLOYMENT");
52+
if (deploymentDescriptor == null) {
53+
// No deployment descriptor in the environment, use a default: two local
54+
// memcached processes configured as two replicas of one shard each.
55+
deploymentDescriptor = "SERVERGROUP1=localhost:11211;SERVERGROUP2=localhost:11212";
56+
}
3957
System.setProperty("EVCACHE_APP1.use.simple.node.list.provider", "true");
40-
System.setProperty("EVCACHE_APP1-NODES", "SERVERGROUP1=localhost:11211;SERVERGROUP2=localhost:11212");
58+
System.setProperty("EVCACHE_APP1-NODES", deploymentDescriptor);
4159
evCache = new EVCache.Builder().setAppName("EVCACHE_APP1").build();
4260
}
4361

4462
/**
45-
* Set a key in memcached
46-
* @throws Exception
63+
* Set a key in the cache.
64+
*
65+
* See the memcached documentation for what "timeToLive" means.
66+
* Zero means "never expires."
67+
* Small integers (under some threshold) mean "expires this many seconds from now."
68+
* Large integers mean "expires at this Unix timestamp" (seconds since 1/1/1970).
69+
* Warranty expires 17-Jan 2038.
4770
*/
4871

49-
public void setKey() throws Exception {
72+
public void setKey(String key, String value, int timeToLive) throws Exception {
5073
try {
5174
Future<Boolean>[] _future = evCache.set(key, value, timeToLive);
52-
//Lets block for write so we don't exit the program fast
75+
76+
// Wait for all the Futures to complete.
77+
// In "verbose" mode, show the status for each.
5378
for (Future<Boolean> f : _future) {
54-
System.out.println("set key " + key + " is successful" + f.get());
79+
boolean didSucceed = f.get();
80+
if (verboseMode) {
81+
System.out.println("per-shard set success code for key " + key + " is " + didSucceed);
82+
}
83+
}
84+
if (!verboseMode) {
85+
// Not verbose. Just give one line of output per "set," without a success code
86+
System.out.println("finished setting key " + key);
5587
}
5688
} catch (EVCacheException e) {
5789
e.printStackTrace();
5890
}
5991
}
6092

6193
/**
62-
* Get the key you have set in memcached
63-
* @return String
94+
* Get the data for a key from the cache. Returns null if the key
95+
* could not be retrieved, whether due to a cache miss or errors.
6496
*/
6597

66-
public String getKey() {
98+
public String getKey(String key) {
6799
try {
68100
String _response = evCache.<String>get(key);
69101
return _response;
70102
} catch (Exception e) {
71103
e.printStackTrace();
104+
return null;
72105
}
73-
return null;
74106
}
75107

76108
/**
77-
* Main Program to set and get using EVCacheClient
78-
* @param args
109+
* Main Program which does some simple sets and gets.
79110
*/
80111

81112
public static void main(String[] args) {
113+
// set verboseMode based on the environment variable
114+
verboseMode = ("true".equals(System.getenv("EVCACHE_SAMPLE_VERBOSE")));
115+
116+
if (verboseMode) {
117+
System.out.println("To run this sample app without using Gradle:");
118+
System.out.println("java -cp " + System.getProperty("java.class.path") + " com.netflix.evcache.sample.EVCacheClientSample");
119+
}
120+
82121
try {
83122
EVCacheClientSample evCacheClientSample = new EVCacheClientSample();
84-
evCacheClientSample.setKey();
85-
Thread.sleep(1000);
86-
System.out.println(" Key returned is >> " + evCacheClientSample.getKey());
87-
System.exit(0);
123+
124+
// Set ten keys to different values
125+
for (int i = 0; i < 10; i++) {
126+
String key = "key_" + i;
127+
String value = "data_" + i;
128+
// Set the TTL to 24 hours
129+
int ttl = 86400;
130+
evCacheClientSample.setKey(key, value, ttl);
131+
}
132+
133+
// Do a "get" for each of those same keys
134+
for (int i = 0; i < 10; i++) {
135+
String key = "key_" + i;
136+
String value = evCacheClientSample.getKey(key);
137+
System.out.println("Get of " + key + " returned " + value);
138+
}
88139
} catch (Exception e) {
89140
e.printStackTrace();
90141
}
142+
143+
// We have to call System.exit() now, because some background
144+
// threads were started without the "daemon" flag. This is
145+
// probably a mistake somewhere, but hey, this is only a sample app.
146+
System.exit(0);
91147
}
92148
}

0 commit comments

Comments
 (0)