|
7 | 7 |
|
8 | 8 | /**
|
9 | 9 | * Created by senugula on 3/24/16.
|
| 10 | + * Updated by akpratt on 5/13/16. |
10 | 11 | */
|
11 | 12 |
|
12 | 13 | /**
|
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. |
20 | 16 | *
|
| 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 |
21 | 28 | */
|
| 29 | + |
22 | 30 | public class EVCacheClientSample {
|
23 | 31 |
|
24 | 32 | 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; |
28 | 34 |
|
| 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 | + */ |
29 | 50 | 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 | + } |
39 | 57 | 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); |
41 | 59 | evCache = new EVCache.Builder().setAppName("EVCACHE_APP1").build();
|
42 | 60 | }
|
43 | 61 |
|
44 | 62 | /**
|
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. |
47 | 70 | */
|
48 | 71 |
|
49 |
| - public void setKey() throws Exception { |
| 72 | + public void setKey(String key, String value, int timeToLive) throws Exception { |
50 | 73 | try {
|
51 | 74 | 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. |
53 | 78 | 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); |
55 | 87 | }
|
56 | 88 | } catch (EVCacheException e) {
|
57 | 89 | e.printStackTrace();
|
58 | 90 | }
|
59 | 91 | }
|
60 | 92 |
|
61 | 93 | /**
|
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. |
64 | 96 | */
|
65 | 97 |
|
66 |
| - public String getKey() { |
| 98 | + public String getKey(String key) { |
67 | 99 | try {
|
68 | 100 | String _response = evCache.<String>get(key);
|
69 | 101 | return _response;
|
70 | 102 | } catch (Exception e) {
|
71 | 103 | e.printStackTrace();
|
| 104 | + return null; |
72 | 105 | }
|
73 |
| - return null; |
74 | 106 | }
|
75 | 107 |
|
76 | 108 | /**
|
77 |
| - * Main Program to set and get using EVCacheClient |
78 |
| - * @param args |
| 109 | + * Main Program which does some simple sets and gets. |
79 | 110 | */
|
80 | 111 |
|
81 | 112 | 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 | + |
82 | 121 | try {
|
83 | 122 | 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 | + } |
88 | 139 | } catch (Exception e) {
|
89 | 140 | e.printStackTrace();
|
90 | 141 | }
|
| 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); |
91 | 147 | }
|
92 | 148 | }
|
0 commit comments