Search code examples
javagoogle-app-enginememcachedjpqllow-latency

Memcache Latency Time Too High?


I have below code working on GAE. When getting the items from memcache I get a latency around 30 ms for 5 to 20 results only. Isn't it too high? Is there anyway I can improve it?

        @SuppressWarnings({ "unchecked"})
        @ApiMethod(name = "queryDesire2")
        public List<String> queryDesire2(
                @Nullable @Named("name2") String name2){
            EntityManager mgr = getEntityManager();
            List<String> list1=new ArrayList<String>();
            Key userKey=null;
            Long x=0L;
            List<Key> keysNew = new ArrayList<Key>();
            MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();

            try {
                    userKey=KeyFactory.createKey(name2,1);
                    if (memcache.get(userKey)==null) {
                        Query query2 = mgr.createQuery("select d.good from Desire d where d.ctgry = :name2");        
                        list1 = query2.setParameter("name2", name2).getResultList();          

                        for (String d : list1){
                        x=x+1L;
                        userKey = KeyFactory.createKey(name2,x);                            
                        memcache.put(userKey, d);
                        memcache.put(name2+"no", x);
                        }
                    } else {
                        Long y=(Long) memcache.get(name2+"no");
                    for (x = 1L; x<=y; x++){
                        userKey=KeyFactory.createKey(name2,x);
                        keysNew.add(userKey);
                    }   
                        Map mMap;
                        mMap = memcache.getAll(keysNew);
                        list1 = new ArrayList<String>(mMap.values());
                    }

             }
            finally {
                mgr.close();
            }
            return list1;


        } 

EDIT:

enter image description here

I captured this snapshot from https://appengine.google.com/... logs. I assumed that 26ms on the top one is the latency. But also I realized there is cpu_usd: 0.000035. What my real concern is to understand how much instance hour will my application use. Is there any formula or way to calculate that?


Solution

  • You can put all objects in a single call. You can also use AsyncMemcacheService - it will work a little faster with put calls.

    Just remember:

    The "multi" batch operations can have any number of elements. The total size of the call and the total size of the data fetched must not exceed 32 megabytes.