Anish
Anish

Reputation: 912

Querying python memcached data

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

Upvotes: 0

Views: 779

Answers (3)

Anish
Anish

Reputation: 912

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

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599630

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.

Upvotes: 2

flycee
flycee

Reputation: 12716

  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.

Upvotes: 0

Related Questions