/** Performs lazy initialization on the cache */ @Override public void onCacheLoad(List<CastViewObject> listObjects) { this.listCastedObjects = listObjects; MemcacheService cache = MemcacheServiceFactory.getMemcacheService(CACHE_KEY + getStrategyName()); cache.clearAll(); }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public String addRoute(Route route, @Context HttpServletResponse servletResponse) throws IOException { try { Entity routeEntity = new Entity("Route"); routeEntity.setProperty("originPlaceID", route.getOrigin().getGooglePlaceId()); routeEntity.setProperty("destinationPlaceID", route.getDestination().getGooglePlaceId()); routeEntity.setProperty("duration", route.getDuration()); routeEntity.setProperty("distance", route.getDistance()); routeEntity.setProperty("origin", route.getOrigin().getAddress()); routeEntity.setProperty("destination", route.getDestination().getAddress()); Text mapJsonAsText = route.getMapJsonAsText(); routeEntity.setProperty("routeJSON", mapJsonAsText); syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); String cacheKey = "route-" + key.nextLong(); syncCache.put(cacheKey, routeEntity); return new JSONObject().append("status", "OK").append("key", cacheKey).toString(); } catch (Exception e) { return new JSONObject() .append("status", "fail") .append("message", "Could not cache object.") .toString(); } }
/** * Returns the Account key associated with the specified authorization key. * * @param pm reference to the persistence manager * @param authorizationKey authorization key to return the account key for * @return the Account key associated with the specified authorization key; or <code>null</code> * if the authorization key is invalid */ public static Key getAccountKeyByAuthKey( final PersistenceManager pm, final String authorizationKey) { final String memcacheKey = CACHE_KEY_AUTH_KEY_ACCOUNT_KEY_PREFIX + authorizationKey; final String accountKeyString = (String) memcacheService.get(memcacheKey); if (accountKeyString != null) return KeyFactory.stringToKey(accountKeyString); final Query q = new Query(Account.class.getSimpleName()); q.setFilter(new FilterPredicate("authorizationKey", FilterOperator.EQUAL, authorizationKey)); q.setKeysOnly(); final List<Entity> entityList = DatastoreServiceFactory.getDatastoreService() .prepare(q) .asList(FetchOptions.Builder.withDefaults()); if (entityList.isEmpty()) return null; final Key accountKey = entityList.get(0).getKey(); try { memcacheService.put(memcacheKey, KeyFactory.keyToString(accountKey)); } catch (final MemcacheServiceException mse) { LOGGER.log(Level.WARNING, "Failed to put key to memcache: " + memcacheKey, mse); // Ignore memcache errors, do not prevent serving user request } return accountKey; }
/** * Persists {@code this} test report to the given {@link * com.google.appengine.api.datastore.DatastoreService} and invalidate the cache. * * @param reports the reports entry point */ public void save(Reports reports) { final Entity entity = new Entity(TEST_REPORT, buildTypeId + buildId); entity.setProperty("buildTypeId", buildTypeId); entity.setProperty("buildId", buildId); entity.setProperty("buildDate", buildDate); entity.setProperty("buildDuration", buildDuration); entity.setProperty("numberOfPassedTests", numberOfPassedTests); entity.setProperty("numberOfIgnoredTests", numberOfIgnoredTests); entity.setProperty("numberOfFailedTests", numberOfFailedTests); final JSONArray jsonArrayFailedTests = new JSONArray(); for (Test oneFailingTest : failedTests) { jsonArrayFailedTests.put(oneFailingTest.asJson()); } entity.setProperty("failedTests", new Text(jsonArrayFailedTests.toString())); final JSONArray jsonArrayIgnoredTests = new JSONArray(); for (Test oneIgnoredTest : ignoredTests) { jsonArrayIgnoredTests.put(oneIgnoredTest.asJson()); } entity.setProperty("ignoredTests", new Text(jsonArrayIgnoredTests.toString())); final DatastoreService datastoreService = reports.getDatastoreService(); datastoreService.put(entity); final MemcacheService memcacheService = reports.getMemcacheService(); memcacheService.delete(buildTypeId); // invalidate cache }
@GET @Path("{route_id}") @Produces(MediaType.APPLICATION_JSON) public String getPlace(@PathParam("route_id") String routeID) { JSONObject reply = new JSONObject(); try { syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); Entity result = (Entity) syncCache.get(routeID); if (result != null) return reply .append("status", "OK") .append("message", "Found in memcache.") .append("Route Object", result) .toString(); return reply.append("status", "fail").append("message", "Not found in memcache.").toString(); } catch (Exception e) { return new JSONObject() .append("status", "fail") .append("message", "Route object with ID " + routeID + " not found.") .toString(); } }
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String fileName = null; long startTime, endTime; String fileContentToDisplay = req.getParameter("FileToDisplay"); res.getWriter().println("File conetent to be displayed : " + fileContentToDisplay); startTime = System.currentTimeMillis(); // Find if the file is in memcache. If present there is no need to go to blobstore to get the // data MemcacheService mService = MemcacheServiceFactory.getMemcacheService(); AppEngineFile readableFile; readableFile = (AppEngineFile) mService.get(fileName); if (readableFile != null) { FileService fService = FileServiceFactory.getFileService(); res.getWriter().println("File " + fileName + " is present in cache"); FileReadChannel readChannel = fService.openReadChannel(readableFile, false); BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8")); String line = null; res.getWriter().println("Contents of the file: "); while ((line = reader.readLine()) != null) { res.getWriter().println(line + "<br>"); } readChannel.close(); } else // Search for the file in blob { List<BlobInfo> blobToRead = new LinkedList<BlobInfo>(); Iterator<BlobInfo> blobIterator = new BlobInfoFactory().queryBlobInfos(); while (blobIterator.hasNext()) blobToRead.add(blobIterator.next()); res.setContentType("text/plain"); Boolean filePresent = false; int i; for (i = 0; i < blobToRead.size(); i++) { fileName = blobToRead.get(i).getFilename(); if (fileName.equals(fileContentToDisplay)) { FileService fService = FileServiceFactory.getFileService(); res.getWriter().println("File " + fileName + " is present in the blob store"); readableFile = fService.getBlobFile(blobToRead.get(i).getBlobKey()); FileReadChannel readChannel = fService.openReadChannel(readableFile, false); BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8")); String line = null; res.getWriter().println("printing the contents : "); while ((line = reader.readLine()) != null) { res.getWriter().println(line); } readChannel.close(); } else { res.getWriter().println("File " + fileName + " is not present in the blob store"); } } } endTime = System.currentTimeMillis(); res.getWriter().println("Time taken for the operation is " + (endTime - startTime) + " ms"); }
/** * Finds the {@code TestReport} with the given build type id ordered by build id in the descendant * order. * * @param buildTypeId the build type id. * @param limit the optional fetch limit, by default {@link * com.google.appengine.tck.site.endpoints.TestReport#DEFAULT_FETCH_LIMIT}. * @param reports the reports entry point * @return the matching test reports list or an empty one if none. */ @SuppressWarnings("unchecked") public static List<TestReport> findByBuildTypeIdOrderByBuildIdDesc( String buildTypeId, Optional<Integer> limit, Reports reports) { final MemcacheService memcacheService = reports.getMemcacheService(); List<TestReport> results = (List<TestReport>) memcacheService.get(buildTypeId); if (results == null) { final Filter buildTypeFilter = new Query.FilterPredicate("buildTypeId", FilterOperator.EQUAL, buildTypeId); final Query query = new Query(TEST_REPORT).setFilter(buildTypeFilter).addSort("buildId", DESCENDING); final DatastoreService datastoreService = reports.getDatastoreService(); final PreparedQuery preparedQuery = datastoreService.prepare(query); final List<Entity> entities = preparedQuery.asList(FetchOptions.Builder.withLimit(limit.or(DEFAULT_FETCH_LIMIT))); results = new ArrayList<>(); for (Entity oneEntity : entities) { final TestReport report = from(oneEntity); results.add(report); } memcacheService.put(buildTypeId, results); } return results; }
@SuppressWarnings("deprecation") public static Entity getEntity(String id) { Entity entity = null; if (syncCache.contains(id)) { entity = (Entity) syncCache.get(id); } if (entity == null) { Query q = new Query("storedString"); // q.setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, id)); q.addFilter("id", Query.FilterOperator.EQUAL, id); PreparedQuery pq = datastore.prepare(q); try { entity = pq.asSingleEntity(); } catch (PreparedQuery.TooManyResultsException e) { Iterator<Entity> iter = pq.asIterator(); while (iter.hasNext()) { Entity ent = iter.next(); if (entity == null) { entity = ent; } else { datastore.delete(ent.getKey()); } } } if (entity != null) { // log.warning("store (because found in datastore) :"+id+" : "+entity.getKey()); syncCache.put(id, entity); } } // log.warning("return :"+id+" : "+(entity!=null?entity.getKey():"")); return entity; }
@PUT @Consumes(MediaType.APPLICATION_XML) public Response putTaskData(String value) { Response res = null; // add your code here // first check if the Entity exists in the datastore DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); Key entKey = KeyFactory.createKey("TaskData", keyname); Date date = new Date(); try { // if it is, signal that we updated the entity Entity ts = datastore.get(entKey); ts.setProperty("value", value); ts.setProperty("date", date); datastore.put(ts); res = Response.noContent().build(); } catch (EntityNotFoundException e) { // if it is not, create it and // signal that we created the entity in the datastore Entity taskdata = new Entity("TaskData", keyname); taskdata.setProperty("value", value); taskdata.setProperty("date", date); datastore.put(taskdata); res = Response.created(uriInfo.getAbsolutePath()).build(); } TaskData td = new TaskData(keyname, value, date); syncCache.put(keyname, td); return res; }
public static void cacheDelete(String nameSpace, Object id) { MemcacheService memcache = cacheInit(nameSpace); try { memcache.delete(id); } catch (MemcacheServiceException e) { // nothing can be done. } }
public boolean incMissedCycleCountForSector(long sectorIndex) { Boolean relevance = (Boolean) memcacheService.get(MC_KEY_SECTOR_RELEVANCE_PREFIX + sectorIndex); boolean result = ((relevance == null) || !relevance); if (result) { memcacheService.increment(MC_KEY_SECTOR_MISSED_CYCLE_COUNT + sectorIndex, 1L, 0L); } return result; }
public static Object cacheGet(String nameSpace, Object id) { Object r = null; MemcacheService memcache = cacheInit(nameSpace); try { r = memcache.get(id); } catch (MemcacheServiceException e) { // nothing can be done. } return r; }
public String newAudioClipInMemcache(String ownerId, AudioClip audioClip) throws BadRequestException { validateUserExistance(ownerId); ArrayList<AudioClip> audioCLipList = (ArrayList<AudioClip>) syncCache.get(TuneInConstants.ALL_AUDIO_CLIPS); if (audioCLipList == null) { audioCLipList = new ArrayList<AudioClip>(); } audioCLipList.add(audioClip); syncCache.put(TuneInConstants.ALL_AUDIO_CLIPS, audioCLipList, Expiration.byDeltaSeconds(60)); return audioClip.getKeyname(); }
public static void cachePutExp(String nameSpace, Object id, Serializable o, int exp) { MemcacheService memcache = cacheInit(nameSpace); try { if (exp > 0) { memcache.put(id, o, Expiration.byDeltaSeconds(exp)); } else { memcache.put(id, o); } } catch (MemcacheServiceException e) { // nothing can be done. } }
/** * Makes sure that the memcache is populated. If the memcache is prepopulated, or this process was * successful in updating memcache from the datastore, return the known value. Otherwise, return * null. */ private Long populateMemcache() { Long result = (Long) memcache.get(memcacheKey); if (result == null) { long max = 0; for (Entry<String, Long> shard : Utilities.scanByPrefix(persistence, prefix, 1000)) { max = Math.max(max, shard.getValue()); } boolean changed = memcache.put(memcacheKey, max, null, SetPolicy.ADD_ONLY_IF_NOT_PRESENT); if (changed) { result = max; } } return result; }
/** * Handles message requests and determines the appropriate response. * * @param req The HttpServletRequest object from doGet/doPost * @param resp The HttpServletResponse object from doGet/doPost * @throws IOException */ private void messageReceived(HttpServletRequest req, HttpServletResponse resp) throws IOException { String username = req.getParameter("usr"); String querytitle = req.getParameter("querytitle"); // add the chat information to memcache MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); Entity entity; String cnt = ""; // Check if the user is in room if (username == null) { resp.sendRedirect("/"); return; } Message message; // Get the message type from the last part of the requested url String type = req.getPathInfo(); Calendar now = Calendar.getInstance(); int hours = now.get(Calendar.HOUR_OF_DAY); int minutes = now.get(Calendar.MINUTE); String time = hours + ":" + minutes; if (isMessage(type)) { message = new Message(CHAT_MESSAGE, username, req.getParameter("message"), time); if (syncCache.get(querytitle) == null) { entity = new Entity("Message", querytitle); entity.setProperty( "content", "[" + time + "] [" + username + "] " + req.getParameter("message") + "<br>"); syncCache.put(querytitle, entity); } else { entity = (Entity) syncCache.get(querytitle); cnt = (String) entity.getProperty("content"); cnt = cnt + "[" + time + "] [" + username + "] " + req.getParameter("message") + "<br>"; entity.setProperty("content", cnt); syncCache.put(querytitle, entity); // System.out.println(cnt); } sendMessage(querytitle, message); } else if (isJoin(type)) { message = new Message(JOIN_MESSAGE, username, "", time); sendMessage(querytitle, message); } else if (isLeave(type)) { message = new Message(LEAVE_MESSAGE, username, "", time); req.getSession().removeAttribute(querytitle); sendMessage(querytitle, message); } }
@Override public GetAccessTokenResult getAccessToken(Iterable<String> scopes) { MemcacheService memcache = MemcacheServiceFactory.getMemcacheService(MEMCACHE_NAMESPACE); String memcacheKey = memcacheKeyForScopes(scopes); GetAccessTokenResult result; Object memcacheResult = memcache.get(memcacheKey); if (memcacheResult != null) { result = (GetAccessTokenResult) memcacheResult; } else { result = getAccessTokenUncached(scopes); Date memcacheExpiration = new Date(result.getExpirationTime().getTime() - 300000); memcache.put(memcacheKey, result, Expiration.onDate(memcacheExpiration)); } return result; }
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { MemcacheService ms; Gson gson; String linkID; List<String> linkList; ms = MemcacheServiceFactory.getMemcacheService(); gson = new Gson(); linkID = req.getParameter("linkid"); linkList = gson.fromJson(req.getParameter("linklist"), List.class); ms.put("LinkList_" + linkID, linkList); }
public static void addToCacheList(String key, String str) { // Add to string list in cache // if (!keycache.contains(key)) { List names = new LinkedList<String>(); names.add(str); keycache.put(key, names); } else { List names = (List) keycache.get(key); if (!names.contains(str)) { names.add(str); keycache.put(key, names); } } }
@DELETE @Path("{route_id}") @Produces(MediaType.APPLICATION_JSON) public String deletePlace(@PathParam("route_id") String routeID) { syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); JSONObject reply = new JSONObject(); try { syncCache.delete(routeID); return reply .append("status", "OK") .append("message", "successfully deleted route " + routeID + " from " + "memcache") .toString(); } catch (Exception e) { return new JSONObject().append("status", "fail").append("message", e.getMessage()).toString(); } }
/** Cast objects: return the list of objects that must be shown in the castable device */ @SuppressWarnings("unchecked") public List<CastViewObject> castObjects(CastView castView) { MemcacheService cache = MemcacheServiceFactory.getMemcacheService(CACHE_KEY + getStrategyName()); List<CastViewObject> castViewObjects = (List<CastViewObject>) cache.get(castView.getMnemonic()); if (castViewObjects != null) { return castViewObjects; } else { castViewObjects = loadObjects(castView); cache.put( castView.getMnemonic(), castViewObjects, Expiration.byDeltaSeconds(EXPIRATION_TIME)); } return castViewObjects; }
@ApiMethod( name = "helloWithCachedCounter", path = "greetingCached", httpMethod = ApiMethod.HttpMethod.GET) public GreetingImpl helloWithCachedCounter() { GreetingImpl greeter = new GreetingImpl(); MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService(); Integer cnt = (Integer) memcacheService.get(Constants.MEMCACHE_COUNTER_KEY); if (cnt == null) { cnt = new Integer(0); } cnt++; memcacheService.put(Constants.MEMCACHE_COUNTER_KEY, cnt); greeter.setMessage("Hello! You have got this message " + cnt + " times."); return greeter; }
public void enableRelevanceForSector(long sectorIndex) { memcacheService.put( MC_KEY_SECTOR_RELEVANCE_PREFIX + sectorIndex, true, Expiration.byDeltaMillis(SECTOR_RELEVANCE_EXPIRATION_MILLIS), MemcacheService.SetPolicy.SET_ALWAYS); }
public int getMissedCycleCountForSector(long sectorIndex) { Number missedCycleCountNum = (Number) memcacheService.get(MC_KEY_SECTOR_MISSED_CYCLE_COUNT + sectorIndex); int missedCycleCount = 0; if (missedCycleCountNum != null) { missedCycleCount = ((Number) missedCycleCountNum).intValue(); } return missedCycleCount; }
/** * Returns the Mouse Practice Game Top scores. * * @return the Mouse Practice Game Top scores */ public static List<MousePracticeGameScoreInfo> getMousePracticeTopScores() { @SuppressWarnings("unchecked") List<MousePracticeGameScoreInfo> scoreList = (List<MousePracticeGameScoreInfo>) memcacheService.get(CACHE_KEY_MPG_TOP_SCORES); if (scoreList == null) { PersistenceManager pm = null; try { pm = PMF.get().getPersistenceManager(); final List<MousePracticeGameScore> mousePracticeGameScoreList = new JQBuilder<>(pm, MousePracticeGameScore.class) .desc("score") .range(0, TopScoresServlet.TOP_SCORE_TABLE_SIZE) .get(); scoreList = new ArrayList<MousePracticeGameScoreInfo>(mousePracticeGameScoreList.size()); final Map<String, Integer> userNamePersonRankMap = new HashMap<String, Integer>(); int personRankCounter = 0; for (final MousePracticeGameScore mousePracticeGameScore : mousePracticeGameScoreList) { final MousePracticeGameScoreInfo score = new MousePracticeGameScoreInfo(); score.setUserName(mousePracticeGameScore.getUserName()); score.setScore(mousePracticeGameScore.getScore()); score.setAccuracy(mousePracticeGameScore.getAccuracy()); score.setHits(mousePracticeGameScore.getHits()); score.setRandomSeed(mousePracticeGameScore.getRandomSeed()); Integer personRank = userNamePersonRankMap.get(mousePracticeGameScore.getUserName()); if (personRank == null) userNamePersonRankMap.put( mousePracticeGameScore.getUserName(), personRank = ++personRankCounter); score.setPersonRank(personRank); scoreList.add(score); } } finally { if (pm != null) pm.close(); } memcacheService.put(CACHE_KEY_MPG_TOP_SCORES, scoreList); } return scoreList; }
@Override public void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException { final MemcacheService cacheShared = MemcacheServiceFactory.getMemcacheService(); if (cacheShared.contains(MemCacheKey.activePoints)) { try { final Map<String, Point> points = (Map<String, Point>) cacheShared.get(MemCacheKey.activePoints); cacheShared.delete( MemCacheKey.activePoints); // TODO possible race condition with record value service for (final Point point : points.values()) { TaskFactory.getInstance().startMoveCachedValuesToStoreTask(point); } } catch (InvalidValueException e) { cacheShared.clearAll(); } } }
public static boolean dropEntity(String id) { Entity entity = getEntity(id); if (entity != null) { // log.warning("drop :"+id+ " : "+ entity.getKey()); datastore.delete(entity.getKey()); syncCache.delete(id); return true; } return false; }
@DELETE public void deleteIt() { // delete an entity from the datastore // just print a message upon exception (don't throw) DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); if (syncCache.get(keyname) != null) { syncCache.delete(keyname); } Key entKey = KeyFactory.createKey("TaskData", keyname); try { Entity ent = datastore.get(entKey); datastore.delete(entKey); System.err.println("TaskData deleted in Datastore"); } catch (EntityNotFoundException e) { System.err.println("TaskData not found in Datastore"); } }
// for the application @GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public TaskData getTaskData() { // same code as above method DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); if (syncCache.get(keyname) != null) { TaskData ts = (TaskData) syncCache.get(keyname); return ts; } Key entKey = KeyFactory.createKey("TaskData", keyname); try { Entity ent = datastore.get(entKey); TaskData ts = new TaskData(keyname, (String) ent.getProperty("value"), (Date) ent.getProperty("date")); return ts; } catch (EntityNotFoundException e) { throw new RuntimeException("Get: TaskData with " + keyname + " not found"); } }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { MemcacheService cache = MemcacheServiceFactory.getMemcacheService(); List<String> authors; if (cache.get("authors") == null) { authors = new LinkedList(); } else { authors = (List<String>) cache.get("authors"); } String author = req.getParameter("author"); String message = req.getParameter("message"); authors.add(author); cache.put("authors", authors); cache.put(author, message); req.getRequestDispatcher("/cache.jsp").forward(req, resp); }