Cache + Bottlenose API - need help

47 views
Skip to first unread message

Kiran Subbaraman

unread,
Oct 7, 2015, 11:55:32 PM10/7/15
to web2py-users
I was trying to use Bottlenose's API (thin wrapper on the Amazon Products API), alongwith web2py's Cache.
bottlenose: https://github.com/lionheart/bottlenose

Bottlenose provides callbacks where the caching logic can be introduced.
The code is listed below
cache_disk = CacheOnDisk(folder='path...')
cache_ram = CacheInRam()

def
write_query_to_db(cache_url, data):
   
return cache_ram(cache_url, lambda: cache_disk(cache_url, lambda: data, 3600), 60)

# Return None if the key is not present
def read_query_from_db(cache_url):
   
# How do I read from the cache, the following doesn't seem to work
return cache_ram(cache_url, lambda: cache_disk(cache_url))


amazon
= bottlenose.Amazon(CacheWriter=write_query_to_db,
                           
CacheReader=read_query_from_db, ...)

Any suggestions?
Thanks,
Kiran

Leonel Câmara

unread,
Oct 8, 2015, 3:46:33 AM10/8/15
to web2py-users
The thing is web2py caching logic is different from what bottlenose wants as it never really returns None, it also never really stores values directly it calls a function to get a value when it needs one so you need to make some kind of adapter for it, you were almost there.

I haven't tested it but this should work.

def reader(cache_url):
   
return cache.ram(cache_url, lambda: None, time_expire=None)   # Time expire can be any value you want here (e.g 3600 so bottlenose writes it again in an hour)


def writer(cache_url, response_text):
    cache
.ram(cache_url, lambda: response_text, time_expire=0)   # Time expire needs to be 0 here to make sure it always writes


amazon
= bottlenose.Amazon(CacheWriter=writer,
                           
CacheReader=reader, ....)





Kiran Subbaraman

unread,
Oct 8, 2015, 11:54:28 PM10/8/15
to web...@googlegroups.com
I'll try this out in the next couple of days. Currently, have side-stepped the callback options from bottlenose, and using web2py cache the way it was designed to be used.
Thanks for your suggestions, Leonel.
________________________________________
Kiran Subbaraman
http://subbaraman.wordpress.com/about/
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages