0

I am storing some number of records in python memcach. The object structure is as shown below. How can i access or get records just like querying a database.

class CartDetails(models.Model):
    id              = models.AutoField(primary_key=True)
    item_name       = models.CharField(max_length = 100)
    location        = models.CharField(max_length = 250)
    item_type       = models.ForeignKey(ItemType)
    comments        = models.TextField(max_length = 250,blank=True)
    item_code       = models.CharField(max_length = 100)
    main_cat_id     = models.ForeignKey(MainCategory)

i know i can access the memcache like this

cache.get(<some key>)

I want some thing just like we are quering in Django

<some model>.objects.filter(<conditions>)

i mean is there any way so that i can specify some condition and get the records from memcache. cache.get() #condition like item_name = "some item", item_type = 1 etc

What are the best practices. Any help will be appreciated greatly.

Thanks

Anish
  • 872
  • 5
  • 21
  • 42

3 Answers3

2

You're missing the point of the cache. It's a simple key-value store, you can't do queries on it: that is what the database is for. The equivalent of LINQ in Django is the ORM, which you're already using. But in .NET just as in Django, you can't run arbitrary queries on a cache layer.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0
  1. For cache, like memcache or redis, speed is first and most important thing, so simple key-value pair is involved like what you say:

    cache.get(<some key>)

    or

    redis.hget(<some id>)

  2. I think your intention of make cache query like <some model>.objects.filter(<conditions>) is more frequent and useful in the business model query in the deep backend, not cache layer. Like relational database as mysql or postgresql.

In summary, for cache, care more about making it simpler and faster. And for business work, like order query, you can use complex query like filter expression.

flycee
  • 11,948
  • 3
  • 19
  • 14
  • I understood your point #2, but i was looking for something like a LINQ in .NET. So that i can query the data in cache based on some condition. Because i am storing a table values in the cache – Anish Mar 17 '15 at 06:21
  • Maybe you can describe 'How do you storing a table values in the cache' more specific, for example: In json like '{"id": {"item_name": "aaa", "location": "bbb"}}' – flycee Mar 17 '15 at 08:44
0

I came to know that pydblite can be used to keep the data's in-memory, which will help me to query the in-memory DB like this

for rec in (db("age") > 30) & (db("country") == "France"):
    print rec["name"] 

where as for python cache and redis i can access the in-memory only using a key.

http://pydblite.readthedocs.org/en/latest/index.html

Thanks

Anish
  • 872
  • 5
  • 21
  • 42
  • You do realize that the particular instance you are keeping this "in-memory" database in may not be the instance that later queries it, right? It's not using the shared memcache, it's the per instance memory used. I think you are missing the point of GAE somewhat. – Paul Collingwood Mar 19 '15 at 10:01