Java Memcache & Persistence Manager encapsulation

29 views
Skip to first unread message

Daniel VE

unread,
Sep 20, 2011, 3:00:18 PM9/20/11
to Google App Engine
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;
}

}

Gerald Tan

unread,
Sep 20, 2011, 4:17:35 PM9/20/11
to google-a...@googlegroups.com
I've worked on something similar, but found out I've totally wasted my time when i came across http://code.google.com/p/objectify-appengine/

Try it out, and don't look back

Pascal Voitot Dev

unread,
Sep 20, 2011, 4:19:53 PM9/20/11
to google-a...@googlegroups.com
Yes exactly and if you want something working with other DB also, look at http://www.sienaproject.com
I'm lead dev of this project so don't hesitate to ask questions!

regards
Pascal

On Tue, Sep 20, 2011 at 10:17 PM, Gerald Tan <woeful...@gmail.com> wrote:
I've worked on something similar, but found out I've totally wasted my time when i came across http://code.google.com/p/objectify-appengine/

Try it out, and don't look back

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/53wNpEmilP4J.

To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.

Daniel VE

unread,
Sep 20, 2011, 5:15:15 PM9/20/11
to Google App Engine
Thanks a lot!

On Sep 20, 10:19 pm, Pascal Voitot Dev <pascal.voitot....@gmail.com>
wrote:
> Yes exactly and if you want something working with other DB also, look athttp://www.sienaproject.com
> I'm lead dev of this project so don't hesitate to ask questions!
>
> regards
> Pascal
>
>
>
>
>
>
>
> On Tue, Sep 20, 2011 at 10:17 PM, Gerald Tan <woefulwab...@gmail.com> wrote:
> > I've worked on something similar, but found out I've totally wasted my time
> > when i came acrosshttp://code.google.com/p/objectify-appengine/
Reply all
Reply to author
Forward
0 new messages