Package com.google.appengine.api.memcache

Examples of com.google.appengine.api.memcache.MemcacheService


        }
    }

    public void cacheSet() {
        try {
            MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();
            memcache.put(getCacheKey(), this);
        } catch (MemcacheServiceException e) {
            // Ignore cache problems, nothing we can do.
        }
    }
View Full Code Here


            cleanSession(request);
            return;
        }

        boolean locked = false;
        MemcacheService memcache = null;
        String mutex = MUTEX_BASE + session.getId();
        memcache = MemcacheServiceFactory.getMemcacheService();
        try {
            // try to get lock
            long started = new Date().getTime();
            // non-UIDL requests will try indefinitely
            while (requestType != RequestType.UIDL
                    || new Date().getTime() - started < MAX_UIDL_WAIT_MILLISECONDS) {
                locked = memcache.put(mutex, 1, Expiration.byDeltaSeconds(40),
                        MemcacheService.SetPolicy.ADD_ONLY_IF_NOT_PRESENT);
                if (locked) {
                    break;
                }
                try {
                    Thread.sleep(RETRY_AFTER_MILLISECONDS);
                } catch (InterruptedException e) {
                    logger.finer("Thread.sleep() interrupted while waiting for lock. Trying again. "
                            + e);
                }
            }

            if (!locked) {
                // Not locked; only UIDL can get trough here unlocked: tell
                // client to retry
                response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
                // Note: currently interpreting Retry-After as ms, not sec
                response.setHeader("Retry-After", "" + RETRY_AFTER_MILLISECONDS);
                return;
            }

            // de-serialize or create application context, store in session
            ApplicationContext ctx = getApplicationContext(request, memcache);

            super.service(request, response);

            // serialize
            started = new Date().getTime();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(ctx);
            oos.flush();
            byte[] bytes = baos.toByteArray();

            started = new Date().getTime();

            String id = AC_BASE + session.getId();
            Date expire = new Date(started
                    + (session.getMaxInactiveInterval() * 1000));
            Expiration expires = Expiration.onDate(expire);

            memcache.put(id, bytes, expires);

            DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
            Entity entity = new Entity(AC_BASE, id);
            entity.setProperty(PROPERTY_EXPIRES, expire.getTime());
            entity.setProperty(PROPERTY_DATA, new Blob(bytes));
            ds.put(entity);

        } catch (DeadlineExceededException e) {
            logger.warning("DeadlineExceeded for " + session.getId());
            sendDeadlineExceededNotification(request, response);
        } catch (NotSerializableException e) {
            logger.log(Level.SEVERE, "Not serializable!", e);

            // TODO this notification is usually not shown - should we redirect
            // in some other way - can we?
            sendNotSerializableNotification(request, response);
        } catch (Exception e) {
            logger.log(Level.WARNING,
                    "An exception occurred while servicing request.", e);

            sendCriticalErrorNotification(request, response);
        } finally {
            // "Next, please!"
            if (locked) {
                memcache.delete(mutex);
            }
            cleanSession(request);
        }
    }
View Full Code Here

    List<Key> theRequestKeys = PbKeyUtil.toKeys(requestPb.keys());
    Map<Key, Entity> theData = new HashMap<Key, Entity>();

    // Memcacheにあるものはキャッシュで済ませる
    {
      final MemcacheService memcache = MemvacheDelegate.getMemcache();
      Map<Key, Object> all = memcache.getAll(theRequestKeys); // 存在しなかった場合Keyごと無い
      theData = new HashMap<Key, Entity>();
      for (Key key : all.keySet()) {
        Entity entity = (Entity) all.get(key);
        if (entity != null) {
          theData.put(key, entity);
View Full Code Here

      Key key = PbKeyUtil.toKey(keys.get(i)); // TODO SATO 並び順は保証されている?
      Entity entity = entitys.get(i);
      //Key key = PbKeyUtil.toKey(entity.getEntity().getKey()); TODO これだとhitしなかったときエラーになる
      newMap.put(key, entity);
    }
    MemcacheService memcache = MemvacheDelegate.getMemcache();
    memcache.putAll(newMap);
    log.debug("get from datastore size: " + newMap.size());

    // ここで取れてきているのはキャッシュにないヤツだけなので再構成して返す必要がある
    byte[] requestByte = requestPb.toByteArray();
    String digest = DigestUtils.md5Hex(requestByte);
View Full Code Here

        cached.putAll(newMap);
      } else {
        putUnderTx.put(handle, newMap);
      }
    } else {
      MemcacheService memcache = MemvacheDelegate.getMemcache();
      Map<Key, Entity> newMap = extractCache(requestPb, responsePb);
      memcache.putAll(newMap);
    }
    return null;
  }
View Full Code Here

   * Deleteを行う前の動作として、とりあえずMemcacheからキャッシュを削除する。
   */
  @Override
  public Pair<byte[], byte[]> pre_datastore_v3_Delete(DeleteRequest requestPb) {
    List<Key> keys = PbKeyUtil.toKeys(requestPb.keys());
    MemcacheService memcache = MemvacheDelegate.getMemcache();
    memcache.deleteAll(keys);

    return null;
  }
View Full Code Here

    // datastore_v3#Next を回避するためにprefetchSizeが設定されていない場合大きめに設定する。
    if (requestPb.getCount() == 0) {
      requestPb.setCount(1000);
    }

    final MemcacheService memcache = MemvacheDelegate.getMemcache();
    String memcacheKey = MemcacheKeyUtil.createQueryKey(memcache, requestPb);

    QueryResult response = (QueryResult) memcache.get(memcacheKey);
    if (response != null) {
      return Pair.response(response.toByteArray());
    } else {
      return Pair.request(requestPb.toByteArray());
    }
View Full Code Here

  public byte[] post_datastore_v3_RunQuery(Query requestPb, QueryResult responsePb) {
    if (isIgnoreKind(requestPb.getKind())) {
      return null;
    }

    final MemcacheService memcache = MemvacheDelegate.getMemcache();
    String memcacheKey = MemcacheKeyUtil.createQueryKey(memcache, requestPb);

    // 最大5分しかキャッシュしないようにする
    Expiration expiration = Expiration.byDeltaSeconds(settings.getExpireSecond());
    memcache.put(memcacheKey, responsePb, expiration);
    return null;
  }
View Full Code Here

   * @return 常に null
   * @author vvakame
   */
  @Override
  public Pair<byte[], byte[]> pre_datastore_v3_Put(PutRequest requestPb) {
    final MemcacheService memcache = MemvacheDelegate.getMemcache();
    final Set<String> memcacheKeys = new HashSet<String>();

    final StringBuilder builder = new StringBuilder();
    for (EntityProto entity : requestPb.entitys()) {
      final Reference key = entity.getMutableKey();
      final String namespace = key.getNameSpace();
      final Path path = key.getPath();
      // elements が並んでいるのは親Keyなどがある場合
      // 配列の添字の若い方 = より祖先 末尾 = 本体 末尾のKindを見れば無視すべきかわかる
      List<Element> elements = path.mutableElements();
      Element element = elements.get(elements.size() - 1);
      final String kind = element.getType();
      if (isIgnoreKind(kind)) {
        continue;
      }

      builder.setLength(0);
      String memcacheKey = MemcacheKeyUtil.createKindKey(builder, namespace, kind);

      memcacheKeys.add(memcacheKey);
    }
    // memcache.incrementAll(memcacheKeys, 1, 0L);
    // broken method ↑
    for (String key : memcacheKeys) {
      memcache.increment(key, 1, 0L);
    }

    return null;
  }
View Full Code Here

     * @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;
    }
View Full Code Here

TOP

Related Classes of com.google.appengine.api.memcache.MemcacheService

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.