com.google.appengine.api.memcache.MemcacheService Java Examples
The following examples show how to use
com.google.appengine.api.memcache.MemcacheService.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AppstatsMonitoredServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { try { URLFetchServiceFactory.getURLFetchService() .fetchAsync(new URL("http://www.example.com/asyncWithGet")) .get(); } catch (Exception anythingMightGoWrongHere) { // fall through } URLFetchServiceFactory.getURLFetchService() .fetchAsync(new URL("http://www.example.com/asyncWithoutGet")); MemcacheService memcache = MemcacheServiceFactory.getMemcacheService(); String randomKey = "" + Math.random(); String randomValue = "" + Math.random(); memcache.put(randomKey, randomValue); resp.setContentType("text/plain"); resp.getOutputStream().println(randomKey); }
Example #2
Source File: ShardedCounterServiceImpl.java From appengine-counter with Apache License 2.0 | 6 votes |
/** * Default Constructor for Dependency-Injection. * * @param memcacheService * @param config The configuration for this service */ public ShardedCounterServiceImpl(final MemcacheService memcacheService, final ShardedCounterServiceConfiguration config, final CounterNameValidator counterNameValidator) { Preconditions.checkNotNull(memcacheService, "Invalid memcacheService!"); this.memcacheService = memcacheService; Preconditions.checkNotNull(config); this.config = config; Preconditions.checkArgument(config.getNumInitialShards() > 0, "Number of Shards for a new CounterData must be greater than 0!"); if (config.getRelativeUrlPathForDeleteTaskQueue() != null) { // The relativeUrlPathForDeleteTaskQueue may be null, but if it's non-null, then it must not be blank. Preconditions.checkArgument(!StringUtils.isBlank(config.getRelativeUrlPathForDeleteTaskQueue()), "Must be null (for the Default Queue) or a non-blank String!"); } Preconditions.checkNotNull(counterNameValidator); this.counterNameValidator = counterNameValidator; }
Example #3
Source File: MemcacheAsync2Test.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testAsyncWithNamespace() { String namespace = "namespace-123"; AsyncMemcacheService ams = MemcacheServiceFactory.getAsyncMemcacheService(namespace); String key = "some-random-key"; String value = "some-random-value"; waitOnFuture(ams.put(key, value)); // non-namespaced lookup Assert.assertFalse(memcache.contains(key)); MemcacheService ms = MemcacheServiceFactory.getMemcacheService(namespace); Assert.assertEquals(value, ms.get(key)); }
Example #4
Source File: TestReport.java From appengine-tck with Apache License 2.0 | 6 votes |
/** * 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; }
Example #5
Source File: Mutex.java From yawp with MIT License | 6 votes |
public boolean aquire() { MemcacheService memcache = MemcacheServiceFactory.getMemcacheService(ns); long start = System.currentTimeMillis(); while (true) { if (memcache.increment(key, 1L, 0L) == 1L) { return true; } if (System.currentTimeMillis() - start > maxWait) { return false; } try { Thread.sleep(100L); } catch (InterruptedException e) { } } }
Example #6
Source File: MemcacheTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testNamespace() { // memcache.setNamespace() is deprecated. MemcacheService otherMemcache = MemcacheServiceFactory.getMemcacheService("other"); otherMemcache.clearAll(); memcache.clearAll(); String key = createTimeStampKey("testNamespace"); otherMemcache.put(key, "default"); assertNull("This key should not exist in the default namespace.", memcache.get(key)); assertNotNull(otherMemcache.get(key)); String key2 = createTimeStampKey("testNamespace2"); memcache.put(key2, "default2"); assertNull("This key should not exist in the other namespace.", otherMemcache.get(key2)); assertNotNull(memcache.get(key2)); }
Example #7
Source File: MemcacheBestPracticeServlet.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { String path = req.getRequestURI(); if (path.startsWith("/favicon.ico")) { return; // ignore the request for favicon.ico } MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); byte[] whoKey = "who".getBytes(); byte[] countKey = "count".getBytes(); byte[] who = (byte[]) syncCache.get(whoKey); String whoString = who == null ? "nobody" : new String(who); resp.getWriter().print("Previously incremented by " + whoString + "\n"); syncCache.put(whoKey, "Java".getBytes()); Long count = syncCache.increment(countKey, 1L, 0L); resp.getWriter().print("Count incremented by Java = " + count + "\n"); }
Example #8
Source File: MemcacheTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testPutAllAddOnlyIfNotPresent() { HashMap<Object, Object> firstValues = new HashMap<Object, Object>(); firstValues.put("key1", "firstValue1"); firstValues.put("key2", "firstValue2"); HashMap<Object, Object> secondValues = new HashMap<Object, Object>(); secondValues.put("key1", "secondValue1"); secondValues.put("key2", "secondValue2"); memcache.putAll(firstValues); memcache.putAll(secondValues, null, MemcacheService.SetPolicy.ADD_ONLY_IF_NOT_PRESENT); for (Map.Entry<Object, Object> entry : firstValues.entrySet()) { assertEquals(entry.getValue(), memcache.get(entry.getKey())); } }
Example #9
Source File: SetAnnouncementServlet.java From ud859 with GNU General Public License v3.0 | 6 votes |
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Query for conferences with less than 5 seats lef Iterable<Conference> iterable = ofy().load().type(Conference.class) .filter("seatsAvailable <", 5) .filter("seatsAvailable >", 0); List<String> conferenceNames = new ArrayList<>(0); for (Conference conference : iterable) { conferenceNames.add(conference.getName()); } if (conferenceNames.size() > 0) { StringBuilder announcementStringBuilder = new StringBuilder( "Last chance to attend! The following conferences are nearly sold out: "); Joiner joiner = Joiner.on(", ").skipNulls(); announcementStringBuilder.append(joiner.join(conferenceNames)); MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService(); memcacheService.put(Constants.MEMCACHE_ANNOUNCEMENTS_KEY, announcementStringBuilder.toString()); } response.setStatus(204); }
Example #10
Source File: MemcacheTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testPutIfUntouchedExpire() { final String TS_KEY = createTimeStampKey("testPutIfUntouched"); memcache.put(TS_KEY, STR_VALUE); MemcacheService.IdentifiableValue oldOriginalIdValue = memcache.getIdentifiable(TS_KEY); final String NEW_VALUE = "new-" + STR_VALUE; // Store NEW_VALUE if no other value stored since oldOriginalIdValue was retrieved. // memcache.get() has not been called, so this put should succeed. Boolean valueWasStored = memcache.putIfUntouched(TS_KEY, oldOriginalIdValue, NEW_VALUE, Expiration.byDeltaSeconds(1)); assertEquals(true, valueWasStored); assertEquals(NEW_VALUE, memcache.get(TS_KEY)); // Value should not be stored after expiration period. sync(3000); assertNull(memcache.get(TS_KEY)); }
Example #11
Source File: MemcacheAsyncTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testPutAllAddOnlyIfNotPresent() { HashMap<Object, Object> firstValues = new HashMap<Object, Object>(); firstValues.put("key1", "firstValue1"); firstValues.put("key2", "firstValue2"); HashMap<Object, Object> secondValues = new HashMap<Object, Object>(); secondValues.put("key1", "secondValue1"); secondValues.put("key2", "secondValue2"); unwrap(service.putAll(firstValues)); unwrap(service.putAll(secondValues, null, MemcacheService.SetPolicy.ADD_ONLY_IF_NOT_PRESENT)); for (Map.Entry<Object, Object> entry : firstValues.entrySet()) { assertEquals(entry.getValue(), unwrap(service.get(entry.getKey()))); } }
Example #12
Source File: MemcacheCommonServiceTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test /** * Call getNamespace() via BaseMemcacheService interface for method coverage reporting. */ public void testGetNamespace() { assertNull("Default namespace should be null.", ((BaseMemcacheService) memcache).getNamespace()); assertNull("Default namespace should be null.", ((BaseMemcacheService) asyncMemcache).getNamespace()); String cacheName = "My-CacheNamespaceForSpecialCase.09"; MemcacheService namedMemcache = MemcacheServiceFactory.getMemcacheService(cacheName); assertEquals(cacheName, ((BaseMemcacheService) namedMemcache).getNamespace()); }
Example #13
Source File: MemcacheAsyncTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testPutIfUntouched() { unwrap(service.put("key", "value")); MemcacheService.IdentifiableValue identifiable = unwrap(service.getIdentifiable("key")); boolean valueWasStored = unwrap(service.putIfUntouched("key", identifiable, "newValue")); assertTrue(valueWasStored); assertEquals("newValue", unwrap(service.get("key"))); boolean valueWasStored2 = unwrap(service.putIfUntouched("key", identifiable, "newestValue")); assertFalse(valueWasStored2); assertEquals("newValue", unwrap(service.get("key"))); }
Example #14
Source File: MemcacheAsyncTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testGetIdentifiables() { unwrap(service.put("key1", "value1")); unwrap(service.put("key2", "value2")); Map<String, MemcacheService.IdentifiableValue> identifiables = unwrap(service.getIdentifiables(Arrays.asList("key1", "key2"))); assertEquals(2, identifiables.size()); assertNotNull(identifiables.get("key1")); assertEquals("value1", identifiables.get("key1").getValue()); assertNotNull(identifiables.get("key2")); assertEquals("value2", identifiables.get("key2").getValue()); }
Example #15
Source File: MemcacheAsyncTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testPutAllReplaceOnlyIfPresent() { HashMap<Object, Object> map = new HashMap<Object, Object>(); map.put("key1", "value1"); map.put("key2", "value2"); unwrap(service.putAll(map, null, MemcacheService.SetPolicy.REPLACE_ONLY_IF_PRESENT)); for (Map.Entry<Object, Object> entry : map.entrySet()) { assertFalse(unwrap(service.contains(entry.getKey()))); } }
Example #16
Source File: DeviceSubscription.java From io2014-codelabs with Apache License 2.0 | 5 votes |
/** * Constructor for DeviceSubscription class. * * @param datastoreService AppEngine datastore service * @param memcacheService AppEngine memcache service */ public DeviceSubscription(DatastoreService datastoreService, MemcacheService memcacheService) { if (datastoreService == null || memcacheService == null) { throw new IllegalArgumentException("datastoreService and memcacheService cannot be null."); } this.datastoreService = datastoreService; this.memcacheService = memcacheService; this.gson = new Gson(); }
Example #17
Source File: MemcacheMultitenancyTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testMemcacheServiceBoundToSpecificNamespaceIgnoresNamespaceManager() { NamespaceManager.set("one"); MemcacheService service = MemcacheServiceFactory.getMemcacheService("two"); service.put("key", "value"); NamespaceManager.set("three"); assertEquals("two", service.getNamespace()); assertTrue(service.contains("key")); assertEquals("value", service.get("key")); }
Example #18
Source File: TestReport.java From appengine-tck with Apache License 2.0 | 5 votes |
/** * 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 }
Example #19
Source File: MemcacheMultitenancyTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testMemcacheServiceNotBoundToSpecificNamespaceAlwaysUsesCurrentNamespace() { MemcacheService service = MemcacheServiceFactory.getMemcacheService(); NamespaceManager.set("one"); service.put("key", "value in namespace one"); NamespaceManager.set("two"); service.put("key", "value in namespace two"); NamespaceManager.set("one"); assertEquals("value in namespace one", service.get("key")); NamespaceManager.set("two"); assertEquals("value in namespace two", service.get("key")); }
Example #20
Source File: MemcacheAsyncTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testPutIfUntouchedMulti() { unwrap(service.put("key1", "value1")); unwrap(service.put("key2", "value2")); MemcacheService.IdentifiableValue identifiable1 = unwrap(service.getIdentifiable("key1")); MemcacheService.IdentifiableValue identifiable2 = unwrap(service.getIdentifiable("key2")); HashMap<Object, MemcacheService.CasValues> map = new HashMap<Object, MemcacheService.CasValues>(); map.put("key1", new MemcacheService.CasValues(identifiable1, "newValue1")); map.put("key2", new MemcacheService.CasValues(identifiable2, "newValue2")); Set<Object> storedKeys = unwrap(service.putIfUntouched(map)); assertEquals(2, storedKeys.size()); assertTrue(storedKeys.contains("key1")); assertTrue(storedKeys.contains("key2")); assertEquals("newValue1", unwrap(service.get("key1"))); assertEquals("newValue2", unwrap(service.get("key2"))); map = new HashMap<Object, MemcacheService.CasValues>(); map.put("key1", new MemcacheService.CasValues(identifiable1, "newestValue1")); map.put("key2", new MemcacheService.CasValues(identifiable1, "newestValue2")); storedKeys = unwrap(service.putIfUntouched(map)); assertTrue(storedKeys.isEmpty()); assertEquals("newValue1", unwrap(service.get("key1"))); assertEquals("newValue2", unwrap(service.get("key2"))); }
Example #21
Source File: MemcacheTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testPutAllReplaceOnlyIfPresent() { HashMap<Object, Object> map = new HashMap<Object, Object>(); map.put("key1", "value1"); map.put("key2", "value2"); memcache.putAll(map, null, MemcacheService.SetPolicy.REPLACE_ONLY_IF_PRESENT); for (Map.Entry<Object, Object> entry : map.entrySet()) { assertFalse(memcache.contains(entry.getKey())); } }
Example #22
Source File: MemcacheTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testGetIdentifiables() { memcache.put("key1", "value1"); memcache.put("key2", "value2"); Map<String, MemcacheService.IdentifiableValue> identifiables = memcache.getIdentifiables(Arrays.asList("key1", "key2")); assertEquals(2, identifiables.size()); assertNotNull(identifiables.get("key1")); assertEquals("value1", identifiables.get("key1").getValue()); assertNotNull(identifiables.get("key2")); assertEquals("value2", identifiables.get("key2").getValue()); }
Example #23
Source File: MemcacheTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testGetSomeIdentifiables() { memcache.put("key1", "value1"); Map<String, MemcacheService.IdentifiableValue> identifiables = memcache.getIdentifiables(Arrays.asList("key1", "no_such_key2")); assertEquals(1, identifiables.size()); assertNotNull(identifiables.get("key1")); assertEquals("value1", identifiables.get("key1").getValue()); }
Example #24
Source File: MemcacheTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testPutIfUntouched() { memcache.put("key", "value"); MemcacheService.IdentifiableValue identifiable = memcache.getIdentifiable("key"); boolean valueWasStored = memcache.putIfUntouched("key", identifiable, "newValue"); assertTrue(valueWasStored); assertEquals("newValue", memcache.get("key")); boolean valueWasStored2 = memcache.putIfUntouched("key", identifiable, "newestValue"); assertFalse(valueWasStored2); assertEquals("newValue", memcache.get("key")); }
Example #25
Source File: MemcacheTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testPutIfUntouchedMulti() { memcache.put("key1", "value1"); memcache.put("key2", "value2"); MemcacheService.IdentifiableValue identifiable1 = memcache.getIdentifiable("key1"); MemcacheService.IdentifiableValue identifiable2 = memcache.getIdentifiable("key2"); HashMap<Object, MemcacheService.CasValues> map = new HashMap<Object, MemcacheService.CasValues>(); map.put("key1", new MemcacheService.CasValues(identifiable1, "newValue1")); map.put("key2", new MemcacheService.CasValues(identifiable2, "newValue2")); Set<Object> storedKeys = memcache.putIfUntouched(map); assertEquals(2, storedKeys.size()); assertTrue(storedKeys.contains("key1")); assertTrue(storedKeys.contains("key2")); assertEquals("newValue1", memcache.get("key1")); assertEquals("newValue2", memcache.get("key2")); map = new HashMap<Object, MemcacheService.CasValues>(); map.put("key1", new MemcacheService.CasValues(identifiable1, "newestValue1")); map.put("key2", new MemcacheService.CasValues(identifiable1, "newestValue2")); storedKeys = memcache.putIfUntouched(map); assertTrue(storedKeys.isEmpty()); assertEquals("newValue1", memcache.get("key1")); assertEquals("newValue2", memcache.get("key2")); }
Example #26
Source File: MemcacheTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testDeleteNoReAddTime() { String key = createTimeStampKey("testDeleteNoReAddTime"); memcache.put(key, STR_VALUE); assertNotNull(memcache.get(key)); // delete and do not allow re-add for another 10 seconds. memcache.delete(key, 10 * 1000); assertNull("key should be null", memcache.get(KEY1)); // re-add should fail since within 10 seconds. assertFalse(memcache.put(key, STR_VALUE, null, MemcacheService.SetPolicy.ADD_ONLY_IF_NOT_PRESENT)); assertNull("key should be null because of policy.", memcache.get(KEY1)); }
Example #27
Source File: MemcacheTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testDeleteAllNoReAddTime() { Map<Object, Object> cacheDat = createSmallBatchData(); memcache.putAll(cacheDat); memcache.deleteAll(cacheDat.keySet(), 10 * 1000); Map<Object, Object> retValues = memcache.getAll(cacheDat.keySet()); assertEquals("All keys should be deleted.", 0, retValues.size()); memcache.putAll(cacheDat, null, MemcacheService.SetPolicy.ADD_ONLY_IF_NOT_PRESENT); Map<Object, Object> retValuesAfter = memcache.getAll(cacheDat.keySet()); assertEquals("No keys should be added because of policy.", 0, retValuesAfter.size()); }
Example #28
Source File: MemcacheAsync2Test.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testDeleteNoReAddTime() { String key = createTimeStampKey("testDeleteNoReAddTime"); memcache.put(key, STR_VALUE); assertNotNull(memcache.get(key)); // delete and do not allow re-add for another 10 seconds. Future<Boolean> future = asyncMemcache.delete(key, 10 * 1000); waitOnFuture(future); assertNull("key should be null", memcache.get(KEY1)); // re-add should fail since within 10 seconds. assertFalse(memcache.put(key, STR_VALUE, null, MemcacheService.SetPolicy.ADD_ONLY_IF_NOT_PRESENT)); assertNull("key should be null because of policy.", memcache.get(KEY1)); }
Example #29
Source File: MemcacheAsync2Test.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testDeleteAllNoReAddTime() { Map<Object, Object> cacheDat = createSmallBatchData(); memcache.putAll(cacheDat); Future<Set<Object>> future = asyncMemcache.deleteAll(cacheDat.keySet(), 10 * 1000); waitOnFuture(future); Map<Object, Object> retValues = memcache.getAll(cacheDat.keySet()); assertEquals("All keys should be deleted.", 0, retValues.size()); memcache.putAll(cacheDat, null, MemcacheService.SetPolicy.ADD_ONLY_IF_NOT_PRESENT); Map<Object, Object> retValuesAfter = memcache.getAll(cacheDat.keySet()); assertEquals("No keys should be added because of policy.", 0, retValuesAfter.size()); }
Example #30
Source File: ShardedCounterServiceCounterShardDecrementTest.java From appengine-counter with Apache License 2.0 | 5 votes |
@Before public void setUp() throws Exception { super.setUp(); final MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService(); final ShardedCounterServiceConfiguration shardedCounterServiceConfiguration = new ShardedCounterServiceConfiguration.Builder() .build(); shardedCounterService = new ShardedCounterServiceImpl(memcacheService, shardedCounterServiceConfiguration); }