Package com.google.appengine.api.memcache

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


    ofy().save().entity(hr).now();
    ofy().clear();
    HasRef fetched = ofy().load().group(HasRef.Foo.class).entity(hr).now();

    // Now try to serialize it in memcache.
    MemcacheService ms = MemcacheServiceFactory.getMemcacheService();
    ms.put("thing", fetched);

    HasRef serialized = (HasRef)ms.get("thing");
    assert serialized.id.equals(hr.id);
    assert serialized.triv.get().getSomeString().equals(t1.getSomeString());
  }
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

      return getDatastoreService().prepare(query);
  }
 
  private Entity getCachedEntity(Key key) {
    try {
      MemcacheService cache = getEntityCache();
      return (Entity) cache.get(key);
    } catch (Exception e) {
      return null;
    }
  }
View Full Code Here

    return null;
  }

  private void cacheEntity(Entity entity) {
    try {
      MemcacheService cache = getEntityCache();   
      cache.put(entity.getKey(), entity);
    } catch (Exception e) {
    }
  }
View Full Code Here

    }
  }
 
  private void purgeCachedEntity(Key key) {
    try {
      MemcacheService cache = getEntityCache();   
      cache.delete(key);
    } catch (Exception e) {
     
    }
  }
View Full Code Here

  public Key<Student> getKey(User user) {
    if (user == null)
      return null;
    // Attempt to fetch from cache
    MemcacheService memcache = dao.memcache();
    Long studentId = (Long)memcache.get(user);
    Key<Student> key = null;

    if (studentId != null && studentId > 0) {
      key = getKey(studentId);
    } else if(!memcache.contains(user)) {
      // If no value or null value in cache, run query
      key = queryByUser(user).getKey();
      studentId = (key == null) ? null : key.getId();
      // Cache result
      memcache.put(user, studentId);
    }
    return key;
  }
View Full Code Here

  public boolean put(Student student) {
    if (student == null)
      return false;
    dao.ofy().put(student);
    // Store student in cache
    MemcacheService memcache = dao.memcache();
    memcache.put(student.getUser(), student.getId());
    return true;
  }
View Full Code Here

  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {
    resp.setContentType("text/plain");
   
    MemcacheService memcache = Registry.dao().memcache();
    memcache.clearAll();
    resp.getWriter().println("Memcache cleared");
  }
View Full Code Here

   
    Object key = this.getMethodKey(method.toGenericString(), pjp.getArgs());
   
    Object result = null;
    //InternalCache internalCache = this.getCache(cacheName);
    MemcacheService syncCache = this.getCache(cacheName);
   
    if(syncCache == null) {
      LOGGER.info("NO CACHE!!!!! Executing the method with no cache!!");
    } else {
      try {
        result = syncCache.get(key);
      } catch(InvalidValueException e) {
              LOGGER.severe(StackTraceUtil.getStackTrace(e));
      }
    }
   
    if(result != null) {
      LOGGER.info("Returning value with key: " + key + " from cache.");
      return result;
    } else {
      try {
        LOGGER.info("NO CACHE! Invoking the method!");
        result =  pjp.proceed();
        if(cacheCollection) this.cacheCollection(result, pjp.getArgs(), syncCache);
        LOGGER.info("NO CACHE! Inserting result into internal cache with key: " + key);
        syncCache.put(key, result);
        LOGGER.info("NO CACHE! Adding to cache internal cache with name: " + cacheName);
        //this.addCache(cacheName, internalCache);
      } catch(Exception e) {
        LOGGER.severe(StackTraceUtil.getStackTrace(e));
        LOGGER.severe("Memcache KBs: " +
            syncCache.getStatistics().getTotalItemBytes() / 1024);
      }
    }
    return result;
  }
View Full Code Here

    }
       
    Object key = this.getMethodKey(method.toGenericString(),pjp.getArgs());
    Object result = null;
    //InternalCache internalCache = this.getCache(cacheName);
    MemcacheService syncCache = this.getCache(cacheName);
   
    if(syncCache == null) {
      LOGGER.severe("NO CACHE!!!!! Executing the method with no cache!!");
    } else {
      try {
        result = syncCache.get(key);
      } catch(InvalidValueException e) {
              LOGGER.severe(StackTraceUtil.getStackTrace(e));
      }
    }
   
    if(result != null) {
      LOGGER.info("Returning value with key: " + key + " from cache.");
      return result;
    } else {
      try {
        LOGGER.info("NO CACHE! Invoking the method!");
        result =  pjp.proceed();
        LOGGER.info("NO CACHE! Inserting result into internal cache with key: " + key);
        syncCache.put(key, result);
        LOGGER.info("(PutCache) Current Namespace: " + syncCache.getNamespace());
        LOGGER.info("NO CACHE! Adding to cache internal cache with name: " + cacheName);
        //this.addCache(cacheName, internalCache);
      } catch(Exception e) {
        LOGGER.severe(StackTraceUtil.getStackTrace(e));
        LOGGER.severe("Memcache KBs: " +
            syncCache.getStatistics().getTotalItemBytes() / 1024);
      }
    }
    return result;
  }
View Full Code Here

TOP

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

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.