/
EVCacheRESTService.java
168 lines (151 loc) · 6.53 KB
/
EVCacheRESTService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.netflix.evcache.service.resources;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.netflix.evcache.EVCache;
import com.netflix.evcache.EVCacheException;
import com.netflix.evcache.EVCacheLatch;
import com.netflix.evcache.EVCacheLatch.Policy;
import com.netflix.evcache.service.transcoder.RESTServiceTranscoder;
import net.spy.memcached.CachedData;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
/**
* Created by senugula on 3/22/16.
*/
@Singleton
@Path("/evcrest/v1.0")
public class EVCacheRESTService {
private static final Logger logger = LoggerFactory.getLogger(EVCacheRESTService.class);
private final EVCache.Builder builder;
private final Map<String, EVCache> evCacheMap;
private final RESTServiceTranscoder evcacheTranscoder = new RESTServiceTranscoder();
@Inject
public EVCacheRESTService(EVCache.Builder builder) {
this.builder = builder;
this.evCacheMap = new HashMap<>();
}
@POST
@Path("{appId}/{key}")
@Consumes({MediaType.APPLICATION_OCTET_STREAM})
@Produces(MediaType.TEXT_PLAIN)
public Response setOperation(final InputStream in, @PathParam("appId") String pAppId, @PathParam("key") String key,
@QueryParam("ttl") String ttl, @DefaultValue("") @QueryParam("flag") String flag) {
try {
final String appId = pAppId.toUpperCase();
final byte[] bytes = IOUtils.toByteArray(in);
return setData(appId, ttl, flag, key, bytes);
} catch (EVCacheException e) {
e.printStackTrace();
return Response.serverError().build();
} catch (Throwable t) {
return Response.serverError().build();
}
}
@PUT
@Path("{appId}/{key}")
@Consumes({MediaType.APPLICATION_OCTET_STREAM})
@Produces(MediaType.TEXT_PLAIN)
public Response putOperation(final InputStream in, @PathParam("appId") String pAppId, @PathParam("key") String key,
@QueryParam("ttl") String ttl, @DefaultValue("") @QueryParam("flag") String flag) {
try {
final String appId = pAppId.toUpperCase();
final byte[] bytes = IOUtils.toByteArray(in);
return setData(appId, ttl, flag, key, bytes);
} catch (EVCacheException e) {
e.printStackTrace();
return Response.serverError().build();
} catch (Throwable t) {
return Response.serverError().build();
}
}
private Response setData(String appId, String ttl, String flag, String key, byte[] bytes) throws EVCacheException, InterruptedException {
final EVCache evcache = getEVCache(appId);
if (ttl == null) {
return Response.status(400).type("text/plain").entity("Please specify ttl for the key " + key + " as query parameter \n").build();
}
final int timeToLive = Integer.valueOf(ttl).intValue();
EVCacheLatch latch = null;
if(flag != null && flag.length() > 0) {
final CachedData cd = new CachedData(Integer.parseInt(flag), bytes, Integer.MAX_VALUE);
latch = evcache.set(key, cd, timeToLive, Policy.ALL_MINUS_1);
} else {
latch = evcache.set(key, bytes, timeToLive, Policy.ALL_MINUS_1);
}
if(latch != null) {
final boolean status = latch.await(2500, TimeUnit.MILLISECONDS);
if(status) {
return Response.ok("Set Operation for Key - " + key + " was successful. \n").build();
} else {
if(latch.getCompletedCount() > 0) {
if(latch.getSuccessCount() == 0){
return Response.serverError().build();
} else if(latch.getSuccessCount() > 0 ) {
return Response.ok("Set Operation for Key - " + key + " was successful in " + latch.getSuccessCount() + " Server Groups. \n").build();
}
} else {
return Response.serverError().build();
}
}
}
return Response.serverError().build();
}
@GET
@Path("{appId}/{key}")
@Produces({MediaType.APPLICATION_OCTET_STREAM})
public Response getOperation(@PathParam("appId") String appId,
@PathParam("key") String key) {
appId = appId.toUpperCase();
if (logger.isDebugEnabled()) logger.debug("Get for application " + appId + " for Key " + key);
try {
final EVCache evCache = getEVCache(appId);
CachedData cachedData = (CachedData) evCache.get(key, evcacheTranscoder);
if (cachedData == null) {
return Response.status(404).type("text/plain").entity("Key " + key + " Not Found in cache " + appId + "\n").build();
}
byte[] bytes = cachedData.getData();
if (bytes == null) {
return Response.status(404).type("text/plain").entity("Key " + key + " Not Found in cache " + appId + "\n").build();
} else {
return Response.status(200).type("application/octet-stream").entity(bytes).build();
}
} catch (EVCacheException e) {
e.printStackTrace();
return Response.serverError().build();
}
}
@DELETE
@Path("{appId}/{key}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces("text/plain")
public Response deleteOperation(@PathParam("appId") String appId, @PathParam("key") String key) {
if (logger.isDebugEnabled()) logger.debug("Get for application " + appId + " for Key " + key);
appId = appId.toUpperCase();
final EVCache evCache = getEVCache(appId);
try {
Future<Boolean>[] _future = evCache.delete(key);
if (_future.equals(Boolean.TRUE)) {
if (logger.isDebugEnabled()) logger.debug("set key is successful");
}
return Response.ok("Deleted Operation for Key - " + key + " was successful. \n").build();
} catch (EVCacheException e) {
e.printStackTrace();
return Response.serverError().build();
}
}
private EVCache getEVCache(String appId) {
EVCache evCache = evCacheMap.get(appId);
if (evCache != null) return evCache;
evCache = builder.setAppName(appId).build();
evCacheMap.put(appId, evCache);
return evCache;
}
}