Google App Engine Memcache Object Types - google-app-engine

Very short questions : Can I only store objects of type Entity in GAE's memcache or does it support other types ?
Thanks

For Java you can use every object that implements java.Serializable as stated in the Javadoc https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/memcache/MemcacheService

For Python: according to the source, any type is acceptable.
def add(self, key, value, time=0, min_compress_len=0, namespace=None):
"""Sets a key's value, iff item is not already in memcache.
Args:
key: Key to set. See docs on Client for details.
value: Value to set. Any type. If complex, will be pickled.
...

Related

Google Cloud Endpoints return value from a method

How can I return a value (i.e. boolean, string, etc.) from either an #endpoint.method or a #MyModel.method (EndpointsModel)?
As per the documentation, you cannot write simple types like String, int, etc. The return types will need to be a POJO, an array or a Collection.
https://developers.google.com/appengine/docs/java/endpoints/paramreturn_types

How to remove value from GAE NDB Property (type BlobKeyProperty)

It might be the most dumb question and my apologies for the same but I am confused
I have the following entity:
class Profile(ndb.Model):
name = ndb.StringProperty()
identifier = ndb.StringProperty()
pic = ndb.BlobKeyProperty() # stores the key to the profile picture blob
I want to delete the "pic" property value of the above entity so that it should look as fresh as if "pic" was never assigned any value. I do not intend to delete the complete entity. Is the below approach correct:
qry = Profile.query(Profile.identifier==identifier)
result_record_list = qry.fetch()
if result_record_list:
result_record_list[0].pic.delete() # or result_record_list[0].pic = none # or undefined or null
I am deleting the actual blob referred by this blob key separately
assign None to it and put it back to the datastore.
result_record_list[0].pic = None
result_record_list[0].put()
The datastore is an OO schemaless databse. So you can add and remove properties from the the Kind (ndb.Model) without the need of a schema update.
If you also want to cleanup the entities look at this anwser from Guido

Datastore: Is it possible to only save to memcache when using ndb API?

dear all
Currently I'm using ndb API to store some statistic information. Unfortunately, this becomes the major source of my cost. I'm thinking it should be much cheaper if I only save them to memcache. It doesn't matter if data is lost due to cache expire.
After read the manual, I assume _use_datastore class variable can be used to configure this behaviour:
class StaticModel(ndb.Model):
_use_datastore = False
userid = ndb.StringProperty()
created_at = ndb.DateTimeProperty(auto_now_add=True)
May I know if above statement is the right solution?
Cheers!
I think there are three ways to achieve what you want.
The first is to set _use_datastore = False on the NDB model class as per your question.
The second would be to pass use_datastore=False whenever you put / get / delete a StaticModel. An example would be:
model = StaticModel(userid="foo")
key = model.put(use_datastore=False)
n = key.get(use_datastore=False)
The third option would be to set a datastore policy in the NDB Context which returns false for any StaticModel keys. Something like:
context.set_datastore_policy(lambda key: True if key.kind() == 'StaticModel' else False)

one to many relationship in Objectify4

I am working with GAE with java. i am just creating a sample application with Student and Course relationships. I am having many branches and many students. Each branch can have many students, i tried like
ObjectifyService.register(Course.class);
ObjectifyService.register(Student.class);
Course course = new Course();
course.setName("ss");
course.setBranch("cs");
ofy().save().entity(course).now();
Student stu = new Student();
stu.setName("student1");
Key<Course> courseKey = new Key<Course>(Course.class, course.getId()); // getting error here
stu.setCourse(courseKey);
System.out.println("saving");
ofy().save().entity(stu).now();
I am not sure how to define this relationship in objecitfy4. I followed the tutorial http://www.eteration.com/objectify-an-easy-way-to-use-google-datastore/
Thanks.
Look at the Javadocs for Key. Instead of a public constructor, there is a more convenient creator method which requires less typing of <> generics:
Key<Course> courseKey = Key.create(Course.class, course.getId());
There are two keys that you might use:
first the GAE low-level com.google.appengine.api.datastore.Key and
second, the objectify's com.googlecode.objectify.Key.
You can use both with Objectify (as under the hood they are ultimately converted to low-level API).
Neither has a public constructor so you can not use new with them.
With low-level keys you'd use KeyFactory.createKey("Course", course.getId()).
With objectify key you'd use com.googlecode.objectify.Key.create(Course.class, course.getId())
When we use Objectify , if we need any Key to be created for condition(KeyFactory.createKey("Course", course.getId()) , In course object, Id field should be specified with index. It will work fine.

Store an object in memcache of GAE in Go

I want to store an object in GAE's memcache using Go. The gae documentation only shows how to store a []byte here: https://developers.google.com/appengine/docs/go/memcache/overview
Of course there are general ways to serialize an object into []byte, by which my task could be accomplished. But by reading the memcache reference, I found there is an "Object" in the memcache Item:
// Object is the Item's value for use with a Codec.
Object interface{}
That seems to be a built-in mechanic to store an object in memcache. However, the gae documentation did not provide a sample code.
Could anyone please show me an example? Thanks in advance
OK, I just figured it out my self. The memcache pkg has two built-in Codec: gob and json. Just use one of them (or of course one can create his own Codec):
var in, out struct {I int;}
// Put in into memcache
in.I = 100
item := &memcache.Item {
Key: "TestKey",
Object: in,
}
memcache.Gob.Set(c, item) // error checking omitted for convenience
// retrieve the value
memcache.Gob.Get(c, "TestKey", &out)
fmt.Fprint(w, out) // will print {100}
Thanks all

Resources