本文整理匯總了Java中com.google.appengine.api.memcache.MemcacheService.put方法的典型用法代碼示例。如果您正苦於以下問題:Java MemcacheService.put方法的具體用法?Java MemcacheService.put怎麽用?Java MemcacheService.put使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.appengine.api.memcache.MemcacheService
的用法示例。
在下文中一共展示了MemcacheService.put方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: detect
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
public Language detect(String str){
String key = str;
String code = "";
// Using the synchronous cache
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
//syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
code = (String) syncCache.get(key); // read from cache
if (code == null) {
System.out.println("Cache miss");
code = detectFromAPI(key);
syncCache.put(key, code);
} else{
System.out.println("Cache hit");
}
String label = getLangaugeLabel(code);
Language lang = new Language(code, label);
return lang;
}
示例2: doGet
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
@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");
}
示例3: countPackages
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
/**
* @return the number of packages
*/
public static int countPackages() {
final String key =
NWUtils.class.getName() + "[email protected]" +
NWUtils.getDataVersion();
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
syncCache.setErrorHandler(ErrorHandlers
.getConsistentLogAndContinue(Level.INFO));
Integer value = (Integer) syncCache.get(key); // read from cache
if (value == null) {
ShardedCounter sc =
ShardedCounter.getOrCreateCounter("NumberOfPackages", 2);
value = sc.getCount();
if (sc.getCount() == 0) {
Objectify ofy = DefaultServlet.getObjectify();
sc.increment(ofy.query(Package.class).count());
value = sc.getCount();
}
syncCache.put(key, value); // populate cache
}
return value;
}
示例4: doGet
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
@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);
}
示例5: doGet
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
@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);
}
示例6: findByBuildTypeIdOrderByBuildIdDesc
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
/**
* 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;
}
示例7: testNamespace
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
@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));
}
示例8: getRequestHash
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
private RequestHash getRequestHash(String requestUri) {
final MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
RequestHash requestHash = (RequestHash) syncCache.get(requestUri);
if (requestHash == null) {
final PersistenceManager pm = PMF.get().getPersistenceManager();
final Query query = pm.newQuery(RequestHash.class);
query.setFilter("requestUri == requestUriParam");
query.declareParameters("String requestUriParam");
query.setUnique(true);
try {
requestHash = (RequestHash) query.execute(requestUri);
if (requestHash != null) {
syncCache.put(requestUri, requestHash, Expiration.byDeltaSeconds(180));
}
} finally {
query.closeAll();
}
}
return requestHash;
}
示例9: newWishlistProductJson
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_JSON)
public void newWishlistProductJson(WishlistProduct product,
@Context HttpServletResponse servletResponse) throws Exception {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user == null) {
System.out.println("Login first");
return;
}
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
Entity entity = new Entity(user.getEmail(), product.getProductID());
entity.setProperty("productName", product.getProductName());
entity.setProperty("currentPrice", product.getCurrentPrice());
entity.setProperty("lowestPrice", product.getLowestPrice());
entity.setProperty("lowestDate", new Date());
datastore.put(entity);
syncCache.put(product.getProductID(), product.getCurrentPrice());
servletResponse.getWriter().println("<html><head>");
servletResponse
.getWriter()
.println(
"<meta http-equiv=\"refresh\" content=\"3;url=/wishlist.jsp\" />");
servletResponse.getWriter().println("</head><body>");
servletResponse.getWriter()
.println(
"<h1>" + product.getProductName()
+ " has been added.</h1><br>");
servletResponse
.getWriter()
.println(
"<h2>Redirecting in 3 seconds...</h2> <a href=\"/wishlist.jsp\">Go back now</a>");
servletResponse.getWriter().println("</body></html>");
servletResponse.flushBuffer();
return;
}
示例10: getLangaugeLabel
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
public String getLangaugeLabel(String lang){
String key = "lang:" + this.target + ":" + lang;
String result = "";
// Using the synchronous cache
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
//syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
result = (String) syncCache.get(key); // read from cache
if (result == null) {
System.out.println("Cache miss");
Hashtable<String, String> hash = getLanguagesFromAPI();
result = hash.get(lang);
// populate cache
Enumeration<String> en = hash.keys();
while(en.hasMoreElements()){
String code = en.nextElement();
String tempkey = "lang:" + this.target + ":" + code;
syncCache.put(tempkey, hash.get(code));
}
} else{
System.out.println("Cache hit");
}
return result;
}
示例11: doGet
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
@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));
String key = "count-sync";
byte[] value;
long count = 1;
value = (byte[]) syncCache.get(key);
if (value == null) {
value = BigInteger.valueOf(count).toByteArray();
syncCache.put(key, value);
} else {
// Increment value
count = new BigInteger(value).longValue();
count++;
value = BigInteger.valueOf(count).toByteArray();
// Put back in cache
syncCache.put(key, value);
}
// Output content
resp.setContentType("text/plain");
resp.getWriter().print("Value is " + count + "\n");
}
示例12: doGet
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
@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
}
String key = "count-concurrent";
// Using the synchronous cache.
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
// Write this value to cache using getIdentifiable and putIfUntouched.
for (long delayMs = 1; delayMs < 1000; delayMs *= 2) {
IdentifiableValue oldValue = syncCache.getIdentifiable(key);
byte[] newValue = oldValue == null
? BigInteger.valueOf(0).toByteArray()
: increment((byte[]) oldValue.getValue()); // newValue depends on old value
resp.setContentType("text/plain");
resp.getWriter().print("Value is " + new BigInteger(newValue).intValue() + "\n");
if (oldValue == null) {
// Key doesn't exist. We can safely put it in cache.
syncCache.put(key, newValue);
break;
} else if (syncCache.putIfUntouched(key, oldValue, newValue)) {
// newValue has been successfully put into cache.
break;
} else {
// Some other client changed the value since oldValue was retrieved.
// Wait a while before trying again, waiting longer on successive loops.
try {
Thread.sleep(delayMs);
} catch (InterruptedException e) {
throw new ServletException("Error when sleeping", e);
}
}
}
}
示例13: getKeys
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
/**
* Returns possible cached result of a query.
*
* @param <T>
* datastore object class
* @param ofy
* Objectify
* @param q
* a query
* @param cacheSuffix
* suffix for the cache key
* @return possible cached result of the query
*/
public static <T> List<Key<T>> getKeys(Objectify ofy,
com.googlecode.objectify.Query<T> q, String cacheSuffix) {
String key = q.toString() + cacheSuffix;
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
syncCache.setErrorHandler(ErrorHandlers
.getConsistentLogAndContinue(Level.INFO));
String value = (String) syncCache.get(key); // read from cache
List<Key<T>> result;
if (value == null) {
result = q.listKeys();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.size(); i++) {
if (i != 0)
sb.append(' ');
sb.append(result.get(i).getString());
}
value = sb.toString();
syncCache.put(key, value); // populate cache
} else {
List<String> keys = NWUtils.split(value, ' ');
result = new ArrayList<Key<T>>();
for (String k : keys) {
result.add(new Key<T>(k));
}
}
return result;
}
示例14: incDataVersion
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
/**
* Increments the version of the data.
*
* @return new version number
*/
public static long incDataVersion() {
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
syncCache.setErrorHandler(ErrorHandlers
.getConsistentLogAndContinue(Level.INFO));
Long v = syncCache.increment("DataVersion", 1L);
if (v == null) {
syncCache.put("DataVersion", 1L);
v = 1L;
}
return v;
}
示例15: getDataVersion
import com.google.appengine.api.memcache.MemcacheService; //導入方法依賴的package包/類
/**
* @return current data version
*/
public static long getDataVersion() {
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
syncCache.setErrorHandler(ErrorHandlers
.getConsistentLogAndContinue(Level.INFO));
Long v = (Long) syncCache.get("DataVersion");
if (v == null) {
syncCache.put("DataVersion", 1L);
v = 1L;
}
return v;
}