/** * 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); } }
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; }
/** * 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; }
@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 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 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; }
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; }
// 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); }
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(); }
// for the browser @GET @Produces(MediaType.TEXT_XML) public TaskData getTaskDataHTML() { // add your code here (get Entity from datastore using this.keyname) // throw new RuntimeException("Get: TaskData with " + keyname + " not found"); // if not found 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"); } }
/** * 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; }
@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 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); } } }
/** 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; }
/** * 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; }
@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"); } }
@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(); } } }
// Display count of entities in an entity group, with consistent caching void showEntityGroupCount( DatastoreService ds, MemcacheService cache, PrintWriter writer, Key entityGroupKey) { EntityGroupCount egCount = (EntityGroupCount) cache.get(entityGroupKey); // Reuses getEntityGroupVersion method from the previous example. if (egCount != null && egCount.version == getEntityGroupVersion(ds, null, entityGroupKey)) { // Cached value matched current entity group version, use that writer.println(egCount.count + " entities (cached)"); } else { // Need to actually count entities. Using a transaction to get a consistent count // and entity group version. Transaction tx = ds.beginTransaction(); PreparedQuery pq = ds.prepare(tx, new Query(entityGroupKey)); int count = pq.countEntities(FetchOptions.Builder.withLimit(5000)); cache.put( entityGroupKey, new EntityGroupCount(getEntityGroupVersion(ds, tx, entityGroupKey), count)); tx.rollback(); writer.println(count + " entities"); } }
public List<AudioClipInstance> getOtherUsersAudioClips(String userId) throws BadRequestException { List<AudioClipInstance> audioClipInstanceList; // no need to check for user existance here! if UserId is null, then all audioClips are returned Filter userFilter = new FilterPredicate(TuneInConstants.AUDIO_CLIP_OWNER_ID, FilterOperator.NOT_EQUAL, userId); Query q = new Query(TuneInConstants.AUDIO_CLIP_TYPE).setFilter(userFilter); PreparedQuery pq = datastore.prepare(q); // check mem-cache for the results first String CACHE_KEY = TuneInConstants.OTHERS_WORK_KEY + userId; audioClipInstanceList = (ArrayList<AudioClipInstance>) syncCache.get(CACHE_KEY); if (audioClipInstanceList == null) { audioClipInstanceList = new ArrayList<AudioClipInstance>(); User user; AudioClipInstance audioClipInstance; for (Entity result : pq.asIterable()) { user = userService.getUserById( (String) result.getProperty(TuneInConstants.AUDIO_CLIP_OWNER_ID)); audioClipInstance = new AudioClipInstance( KeyFactory.keyToString(result.getKey()), (String) result.getProperty(TuneInConstants.AUDIO_CLIP_TITLE), user, (String) result.getProperty(TuneInConstants.AUDIO_CLIP_AUDIO_ID), (String) result.getProperty(TuneInConstants.AUDIO_CLIP_IMAGE_ID), (Date) result.getProperty(TuneInConstants.AUDIO_CLIP_DATE)); audioClipInstanceList.add(audioClipInstance); } syncCache.put(CACHE_KEY, audioClipInstanceList, Expiration.byDeltaSeconds(300)); } return audioClipInstanceList; }
private List<String> getListFromCacahe(String key) { return (List) keycache.get(key); }
/** * Returns the download stat info for the specified file. * * @param fileName file name to return download stat info for * @return the download stat info for the file name */ public static DownloadStatInfo getDownloadStatByFile(final String fileName) { return (DownloadStatInfo) memcacheService.get(CACHE_KEY_FILE_DOWNLOAD_STAT_PREFIX + fileName); }
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/html"); resp.getWriter().println("<html><body>"); String keyname = req.getParameter("keyname"); String value = req.getParameter("value"); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); // Using the synchronous cache. MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); // display every element of kind TaskData for /datastore if (req.getParameterMap().isEmpty()) { // querying from datastore resp.getWriter().println("<h3>Datastore results:</h3>"); List<String> listOfKeys = new ArrayList<String>(); Query q = new Query("TaskData"); PreparedQuery pq = datastore.prepare(q); for (Entity result : pq.asIterable()) { String datastore_key = result.getKey().getName(); String taskData_value = (String) result.getProperty("value"); Date taskData_date = (Date) result.getProperty("date"); resp.getWriter() .println( "<p>keyname = " + datastore_key + " value = " + taskData_value + " date = " + taskData_date.toString() + "</p>"); listOfKeys.add(datastore_key); } // check which of the keys exist in memcache String memcache_value; resp.getWriter().println("<h3>Memcache results:</h3>"); for (String datastore_key : listOfKeys) { memcache_value = (String) syncCache.get(datastore_key); if (memcache_value != null) { // String decoded = new String(memcache_value, "UTF-8"); resp.getWriter() .println("<p>keyname = " + datastore_key + " value = " + memcache_value + "</p>"); } } } // display element of kind TaskData with key=keyname else if (keyname != null && value == null) { // first check in the cache String memcache_value = (String) syncCache.get(keyname); // Read from cache. // Get value from datastore Key task_key = KeyFactory.createKey("TaskData", keyname); try { Entity tne = datastore.get(task_key); if (memcache_value == null) { resp.getWriter().println("<h2>Datastore</h2>"); } else { resp.getWriter().println("<h2>Both</h2>"); } } catch (EntityNotFoundException e) { resp.getWriter().println("<h2>Neither</h2>"); } } // store element of kind TaskData with key=keyname and value=value else if (keyname != null && value != null) { Entity tne = new Entity("TaskData", keyname); tne.setProperty("value", value); tne.setProperty("date", new Date()); datastore.put(tne); syncCache.put(keyname, value); // Populate cache. resp.getWriter() .println("<h2>Stored " + keyname + " and " + value + " in Datastore and Memcache</h2>"); } else { resp.getWriter().println("<h2>You entered wrong query parameters</h2>"); } /* Entity tne = new Entity("TaskData", "Person"); alice.setProperty("gender", "female"); alice.setProperty("age", 20); */ resp.getWriter().println("</body></html>"); }
// memcache only endpoints public List<AudioClip> getAudioClipInMemcache() throws BadRequestException { ArrayList<AudioClip> audioClipList = (ArrayList<AudioClip>) syncCache.get(TuneInConstants.ALL_AUDIO_CLIPS); return audioClipList; }
private static void removeFromCacheList(String key, String str) { List names = (List) keycache.get(key); names.remove(str); keycache.put(key, names); }
/** Gets the current value */ public long get() { Long prepopulated = populateMemcache(); return (prepopulated != null) ? prepopulated : (Long) memcache.get(memcacheKey); }