[Feature request] Caching at DAL level

31 views
Skip to first unread message

Praneeth

unread,
Mar 9, 2010, 6:23:13 AM3/9/10
to web...@googlegroups.com, mdip...@cs.depaul.edu
Hello all,

I was wondering if DAL level caching could be added. I am aware of
cache.ram and cache.disk, but these require the usage of a decorator
over the functions. I feel that a cache option to cache at the DAL level
that could be enabled by flipping an option in db.py of an application
would be of great help. The cache can be a key value pair of sorts,
where the cache is flushed on each write/update and cached on the first
read - There are probably smarter ways of doing this.

Thoughts?

--
Praneeth

mdipierro

unread,
Mar 9, 2010, 10:03:12 AM3/9/10
to web2py-users
It is there already.

values=db(...).select(cache=(cache.disk,3600))

3600 is the time.

Praneeth

unread,
Mar 9, 2010, 10:06:20 AM3/9/10
to web...@googlegroups.com
Would it be better to have a non time based caching? Say refresh cache
on DB update?
Time based caching requires guessing the cache time - which in my
opinion seems like a bad thing to do.

Thoughts?

--
Praneeth

mdipierro

unread,
Mar 9, 2010, 11:08:57 AM3/9/10
to web2py-users
You can do that if after insert/update/delete you do

db(...).select(cache=(cache.disk,0))

Massimo

Thadeus Burgess

unread,
Mar 9, 2010, 11:37:13 AM3/9/10
to web...@googlegroups.com
What if the cache system includes a "category" or "type" field. All
cached SQL could be of type "SQL", and then say responses can be
cached as "RESP". This way if you refresh the cache instead of
clearing the entire thing, we can specify

cache.ram.clear(type="SQL") and it will only clear the sql typed
entries, leaving other aspects of the cache alone.

however cache.ram.clear() will still clear everything.

That way, the DAL could be set a flag to clear its own cache upon
i/u/d operations.

How does this sound? If good I can start on the patch right away :)

-Thadeus

> --
> You received this message because you are subscribed to the Google Groups "web2py-users" group.
> To post to this group, send email to web...@googlegroups.com.
> To unsubscribe from this group, send email to web2py+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/web2py?hl=en.
>
>

mdipierro

unread,
Mar 9, 2010, 12:00:18 PM3/9/10
to web2py-users
You can already do

cache.ram.clear(db._uri + '/SELECT')

and it will clear all cached select statements. You have to specify
db._uri because you may have multiple db cached.

Massimo

On Mar 9, 10:37 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
> What if the cache system includes a "category" or "type" field. All
> cached SQL could be of type "SQL", and then say responses can be
> cached as "RESP". This way if you refresh the cache instead of
> clearing the entire thing, we can specify
>
> cache.ram.clear(type="SQL") and it will only clear the sql typed
> entries, leaving other aspects of the cache alone.
>
> however cache.ram.clear() will still clear everything.
>
> That way, the DAL could be set a flag to clear its own cache upon
> i/u/d operations.
>
> How does this sound? If good I can start on the patch right away :)
>
> -Thadeus
>

Thadeus Burgess

unread,
Mar 9, 2010, 12:20:15 PM3/9/10
to web...@googlegroups.com
So an option to DAL.__init__(auto_cache=False, clear_cache_on_update=False) ?

auto_cache does an infinity cache time, where if there is any updates
done to the db all cached selects are cleared.... and alternatively an
option to only clear cached selects that have to do with the table
that is selected.

I feel about 50% on the idea... not sure I want every single select I
make automatically cached, you definitely wouldn't want this if your
competing for RAM, but if you can afford the RAM this would be nice.

-Thadeus

Richard

unread,
Mar 10, 2010, 7:45:48 PM3/10/10
to web2py-users
I would also be interested in something like this.

How does the cache decide how much RAM to use?


On Mar 10, 4:20 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
> So an option to DAL.__init__(auto_cache=False, clear_cache_on_update=False) ?
>
> auto_cache does an infinity cache time, where if there is any updates
> done to the db all cached selects are cleared.... and alternatively an
> option to only clear cached selects that have to do with the table
> that is selected.
>
> I feel about 50% on the idea... not sure I want every single select I
> make automatically cached, you definitely wouldn't want this if your
> competing for RAM, but if you can afford the RAM this would be nice.
>
> -Thadeus
>

mdipierro

unread,
Mar 10, 2010, 9:36:35 PM3/10/10
to web2py-users
it does not decide. It tries to cache everything that you ask it to
cache so you should be careful.

Kuba Kucharski

unread,
Mar 11, 2010, 3:58:27 AM3/11/10
to web...@googlegroups.com
you could probably use cache.disk with tmpfs(ram-disk) and this will
result in caching in ram up to limited size
using tmpfs is described here: http://www.web2py.com/AlterEgo/default/show/223

--
Kuba

mdipierro

unread,
Mar 11, 2010, 10:01:48 AM3/11/10
to web2py-users
Nice!

mdipierro

unread,
Mar 11, 2010, 10:14:28 AM3/11/10
to web2py-users
On a second read. Most of it is useful but probably the last line is
not what users want.
The effect would not be to cache db(...).select(cache=....), instead
the effect would be to store the entire database in RAM.

If the server goes down the data is lost.

Web2py already provides this functionality. You can simply do

db=DAL('sqlite:memory:')

Yet is id different from caching.

I think what you want instead if mount the rambased disk as the cache
folder and then do

db(...).select(cache=(ram.disk,3600)....)

so the ram.disk is actually in ram but buffered on disk.

Massimo

On Mar 11, 2:58 am, Kuba Kucharski <kuba.kuchar...@gmail.com> wrote:

Thadeus Burgess

unread,
Mar 11, 2010, 10:34:17 AM3/11/10
to web...@googlegroups.com
Too bad we can specify to cache.disk where we want it to store...

-Thadeus

mdipierro

unread,
Mar 11, 2010, 11:12:35 AM3/11/10
to web2py-users
posting to trunk

On Mar 11, 9:34 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
> Too bad we can specify to cache.disk where we want it to store...
>
> -Thadeus
>

Kuba Kucharski

unread,
Mar 11, 2010, 12:06:30 PM3/11/10
to web...@googlegroups.com

Thx, Massimo, this is exactly what I was trying to say.. I didn't know about 'memory:' when writing my entry, so I will try to edit this alterego, I tried to cover both cases in alterego and in my reply to this thread I was pointing only one of them, that is where confusion came from

On 11 Mar 2010 17:12, "mdipierro" <mdip...@cs.depaul.edu> wrote:

posting to trunk


On Mar 11, 9:34 am, Thadeus Burgess <thade...@thadeusb.com> wrote:

> Too bad we can specify to cach...

> On Thu, Mar 11, 2010 at 9:14 AM, mdipierro <mdipie...@cs.depaul.edu> wrote:

> > On a second read. ...

> > For more options, visit this group athttp://groups.google.com/group/web2py?hl=en.


--
You received this message because you are subscribed to the Google Groups "web2py-users" group....

mdipierro

unread,
Mar 11, 2010, 12:13:30 PM3/11/10
to web2py-users
Anyway, in trunk you can do

from gluon.cache import CacheOnDisk
cache.disk = CacheOnDisk(folder='/the/memory/mapped/folder')

On Mar 11, 11:06 am, Kuba Kucharski <kuba.kuchar...@gmail.com> wrote:
> Thx, Massimo, this is exactly what I was trying to say.. I didn't know about
> 'memory:' when writing my entry, so I will try to edit this alterego, I
> tried to cover both cases in alterego and in my reply to this thread I was
> pointing only one of them, that is where confusion came from
>

Kuba Kucharski

unread,
Mar 11, 2010, 12:33:25 PM3/11/10
to web...@googlegroups.com

Neat.

On 11 Mar 2010 18:13, "mdipierro" <mdip...@cs.depaul.edu> wrote:

Anyway, in trunk you can do

from gluon.cache import CacheOnDisk
cache.disk = CacheOnDisk(folder='/the/memory/mapped/folder')




On Mar 11, 11:06 am, Kuba Kucharski <kuba.kuchar...@gmail.com> wrote:

> Thx, Massimo, this is exa...

> On 11 Mar 2010 17:12, "mdipierro" <mdipie...@cs.depaul.edu> wrote:
>
> posting to trunk
>

> On Mar...

To post to this group, send email to web...@googlegroups.com.

To unsubscribe from this group, send e...

Kuba Kucharski

unread,
Mar 11, 2010, 1:38:23 PM3/11/10
to web...@googlegroups.com
How do I get my security code for editing an alterego?

--
Kuba

mdipierro

unread,
Mar 11, 2010, 2:01:46 PM3/11/10
to web2py-users
I emailed it to you

On Mar 11, 12:38 pm, Kuba Kucharski <kuba.kuchar...@gmail.com> wrote:
> How do I get my security code for editing an alterego?
>
> --
> Kuba
>
> On Thu, Mar 11, 2010 at 6:33 PM, Kuba Kucharski
>

> <kuba.kuchar...@gmail.com> wrote:
> > Neat.

Kuba Kucharski

unread,
Mar 11, 2010, 3:19:13 PM3/11/10
to web...@googlegroups.com
I had edited AlterEgo though slices are so much better nowadays

http://www.web2pyslices.com/main/slices/take_slice/73

--
Kuba

> --
> You received this message because you are subscribed to the Google Groups "web2py-users" group.

> To post to this group, send email to web...@googlegroups.com.

> To unsubscribe from this group, send email to web2py+un...@googlegroups.com.

mdipierro

unread,
Mar 11, 2010, 3:33:00 PM3/11/10
to web2py-users
I agree

On Mar 11, 2:19 pm, Kuba Kucharski <kuba.kuchar...@gmail.com> wrote:
> I had edited AlterEgo though slices are so much better nowadays
>
> http://www.web2pyslices.com/main/slices/take_slice/73
>
> --
> Kuba
>

Reply all
Reply to author
Forward
0 new messages