The easiest way to save value in the Google App Engine Python?

I am lazy! And I just want to keep one string value. All this! Can I jump over any simulation and just save the value?

+4
source share
3 answers

Depending on how long you want to save the information, you can also use memcache:

Import

from google.appengine.api import memcache 

All you need to know to get and set the data:

 memcache.set("key", value) value = memcache.get("key") 

If your data needs long-term progress, you need to use a backend data warehouse. For your data, it is recommended to use both memcache and data storage, since memcache can be cleared at any time. For short-term, ephemeral resistance, memcache does the trick.

More details here: http://code.google.com/appengine/docs/python/memcache/usingmemcache.html

+4
source

As far as I know, DataStore is the only storage available in App Engine. However, this is not a lot of code, even if you are lazy.;)

Make sure you import the db module:

 from google.appengine.ext import db 

Then just save the key / value pair (you can use this model to store any other additional lines you could add):

 class MyData(db.Model): myKey = db.StringProperty(required=True) myValue = db.StringProperty(required=True) data = MyData(myKey="Title", myStringValue="My App Engine Application") data.put() 

You can get it again with

 db.Query(Entry).filter("myKey=", "Title").get().myStringValue 
+8
source

Mark B's solution is good, but you will get better performance if your “key” is the correct object key. Thus, you halve the scan using the necessary data. (In terms of SQL, you will choose by primary key.)

 class Global(db.Model): val = db.StringProperty() @classmethod def get(cls, key): return cls.get_by_key_name(key).val @classmethod def set(cls, key, val): entity = cls(key_name=key, val=val) entity.put() return val 

Now you can get and set using the class method.

  Global.set('foo', 'this is the value for foo') print "The value of foo is: %r" % Global.get('foo') 
+7
source

Source: https://habr.com/ru/post/1302646/


All Articles