Hello,

I'm new using GAE. I'm not sure if the way I'm developing my first app
is correct in the abstraction aproach and usage of this both, the
memcache and persistence manager. Can you give me any feedback? Is
that correct? Thank you!

This is what I do:

- PersistenLayer class -> Singleton of PersistenManagerFactory to get
PersistenManagers

- CacheLayer class -> Singleton Cache class with put and get methods

- PersistentCachedEntity -> A base class to extend from that
encanpsulates the work with memcache and datastore, using the two
previous classes.

- MyClass -> extending PersistentCachedEntity all my classes I want
that work as if objects always were in mem.


This let me works like that way:
--------------------------

MyClass a = MyClass.getById(70);
a.setAttributeA("value 1");
a.update();

MyClass b = new MyClass();
b.setAttributeA("value 1");
b.setAttributeB("value 2");
b.update();
int new_object_id = b.getId();





---------- The code of PersistenCachedEntity class


public class PersistentCachedEntity implements Serializable{


        private static final long serialVersionUID = 3622746186696241522L;

        public PersistentCachedEntity(){        }


        public static PersistentCachedEntity getById(Class<?> what_class,
long id) throws Exception{

                PersistentCachedEntity entity = getCached(what_class,id);

                if(entity==null){
                        entity = getPersistent(what_class,id);
                        if(entity==null){
                                throw new Exception("Persistent Object of
"+what_class.toString()+" not found. ID: "+id);
                        }else{
                                cacheUpdate(entity);
                        }
                }else{
                        System.out.println("From Memcache");
                }

                return entity;
        }


        public long update(){

                try{
                        persistentUpdate(this);
                        cacheUpdate(this);
                }catch(Exception e){
                        System.out.println(e);
                }

                return 1;
        }


        private static void cacheUpdate(PersistentCachedEntity entity) throws
Exception{
                String memkey = entity.getClass().getSimpleName()
+"_ID_"+entity.getId();
                CacheLayer.put(memkey,(Object)entity);
    }

        private static void persistentUpdate(PersistentCachedEntity entity){
                PersistenceManager pm = 
PersistenceLayer.getPersistenceManager();
                try {
                        pm.makePersistent(entity);
                } finally {
                        pm.close();
                }

        }

        private static PersistentCachedEntity getCached(Class<?> what_class,
long id){
                String memkey = what_class.getSimpleName()+"_ID_"+id;
                PersistentCachedEntity entity = (PersistentCachedEntity)
CacheLayer.get(memkey);
                return entity;
        }

        private static PersistentCachedEntity getPersistent(Class<?>
what_class, long id){

                PersistentCachedEntity entity;

                try{
                        PersistenceManager pm = 
PersistenceLayer.getPersistenceManager();
                    entity = (PersistentCachedEntity)
pm.getObjectById(what_class,id);
                        pm.close();
                }catch(JDOObjectNotFoundException exception){

                        entity = null;
                }
                return entity;
        }

        public Key getKey(){
                return null;
        }

        public long getId() throws Exception{

                Key key = getKey();

                if(key!=null){
                        return key.getId();
                }else{
                        throw new Exception("Persistent class has no valid ID. 
Not yet
persisted, update it first.");
                }
        }

}



-----------

------------------------ The MyClass class code


@PersistenceCapable
public class EntityA extends PersistentEntity{

        //Stuff to make persistence works
        ///////////////////////////////////////////////////////
        private static final long serialVersionUID = 1L;

        @PrimaryKey
       @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
       private Key key = null;

        public static EntityA getById(int id) throws Exception{
                return (EntityA) PersistentEntity.getById(EntityA.class, id);
        }

        public Key getKey() {
                return key;
        }


        @Persistent
        private String attribute;


        public void setAttribute(String value) {
                this.attribute = value;
        }

        public String getAttribute() {
                return value;
        }

}

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.

Reply via email to