PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 12, 2022

[FIXED] Why GAE memcache keeps resetting?

 November 12, 2022     google-app-engine, java, memcached     No comments   

Issue

I am using memcache to store only integer value with a key created with a string value. I didn't define expiration value to let the values hang on as long as they can which google documations say. But the problem is oldest item stays there 4 hours at most. Usually less than 2 hours only. Suddenly everything goes zero. Here is the code:

Key mKey=null;
MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();

    try {
        mKey=KeyFactory.createKey("device",deviceNo);
        if (memcache.get(mKey)==null) {
        Query query = mgr.createQuery("select d.insDate from TrialCheck d where d.DeviceId = :deviceNo");
        resultList.addAll(query.setParameter("deviceNo", deviceNo).getResultList());
            if (resultList.size()<1){
                resultList.add(0);
            } else {
                for (Integer d:resultList) {
                    memcache.put(mKey, d);
                }
            }
        } else {
            Integer installDate = (Integer) memcache.get(mKey);
            resultList.add(installDate);
        }

I read some posts saying that it is about instance going down but I couldn't see any correlation between the instance and memcache reset. Is this behaviour normal for shared memcache or the code has problem?


Solution

Shared memcache provides cache capacity on a best-effort basis and is subject to the overall demand of all applications served by App Engine. Dedicated memcache provides a fixed cache capacity assigned exclusively to your application.

https://cloud.google.com/appengine/docs/adminconsole/memcache

Also in your code, you get the item twice out of cache. You call memcache.get(mKey) to check if it is null. If not null, you get it out of cache again and cast to Integer. Set the first memcache.get(mKey) to a variable that you can use later as you may run into the chance of the cache expiring between the first and second get.



Answered By - Jeff Deskins
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing