开发者

How to update data in memcache when persistent object is updated in GAE

开发者 https://www.devze.com 2022-12-21 13:56 出处:网络
Perhaps I\'m missing something simple, as I believe that this is a common scenario... When I am working with an object retrieved from the datastore, I want to detect any changes to the object and upd

Perhaps I'm missing something simple, as I believe that this is a common scenario...

When I am working with an object retrieved from the datastore, I want to detect any changes to the object and update the memcache. In a non-JDO scenario this would be easy, as any data logic layer would intercept all updates and thus have a chance to update the memcache.

However, with JDO, updates are achieved by updating attached obje开发者_开发百科cts and then letting the persistencemanager do the rest when it closes, and thus my code is never notified of updates. I could put events into all of my getters and setters to be notified of changes to my objects, but I would rather avoid that.

Any clues about how this is normally done would be appreciated.


You can add a preUpdate hook to your JDO annotated objects. This sounds like it would work for your use case.

Example code from the link:

  @Entity
  public class Thing {
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     // Your Primary Key

     // Getters, setters, constructors, oh my!

     @PrePersist
     @PreUpdate
     public void prePersist() {
       // get JCache client instance
       // serialize object
       //store in cache
     }
} 

Edit: Oops, that's a JPA example. The link has JDO examples.

0

精彩评论

暂无评论...
验证码 换一张
取 消