有没有办法为Class :: DBI缓存机制? [英] Is there a way to caching mechanism for Class::DBI?

查看:98
本文介绍了有没有办法为Class :: DBI缓存机制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组相当复杂的ORM模块,它们继承自 Class :: DBI .由于数据很少更改,因此我考虑在此之上使用高速缓存/存储层来加快处理速度.我找到了一个模块: Class :: DBI :: Cacheable 但没有评级或RT上的任何评论.我希望听到使用过此或任何其他Class :: DBI缓存方案的人员的来信.

I have a set of rather complex ORM modules that inherit from Class::DBI. Since the data changes quite infrequently, I am considering using a Caching/Memoization layer on top of this to speed things up. I found a module: Class::DBI::Cacheable but no rating or any reviews on RT. I would appreciate hearing from people who have used this or any other Class::DBI caching scheme.

一吨.

推荐答案

我也很讨厌重复自己的ORM!如果您所有的获取都通过单个api(或其子类)进行,则缓存/记忆化非常容易.

I too have rolled my own ORM plenty of times I hate to say! Caching/Memoization is pretty easy if all your fetches happen through a single api (or subclasses thereof).

对于基于唯一键的任何提取,您都可以仅基于键的串联进行缓存.一个幼稚的方法可能是:

For any fetch based on a unique key you can just cache based on a concatenation of the keys. A naive approach might be:

my %_cache;

sub get_object_from_db {
    my ($self, $table, %table_lookup_key) = @_;

    # concatenate a unique key for this object
    my $cache_key = join('|', map { "$_|$table_lookup_key{$_}" }
                       sort keys %table_lookup_key

    return $_cache{$cache_key}
        if exists $_cache{$cache_key};

    # otherwise get the object from the db and cache it in the hash
    # before returning
}

您可以使用CPAN上的Cache ::模块套件代替哈希来实现缓存中的时间和内存限制.

Instead of a hash, you can use the Cache:: suite of modules on CPAN to implement time and memory limits in your cache.

如果要缓存一段时间,则可能需要考虑一种使缓存中的对象过期的方法.例如,如果所有更新也都通过ORM,则可以清除(或更新)您的update()ORM方法中的缓存条目.

If you're going to cache for some time you might want to think about a way to expire objects in the cache. If for instance all your updates also go through the ORM you can clear (or update) the cache entry in your update() ORM method.

要仔细考虑的最后一点-每次都返回具有含义的相同对象.例如,如果一段代码检索了一个对象并更新了一个值,但没有将该更改提交给数据库,则其他所有检索该对象的代码都将看到该更改.如果将一系列操作串联在一起,这可能非常有用-它们可以全部更新对象,然后可以在最后提交它-但这可能不是您想要的.我通常会在从数据库中获取对象时在该对象上设置一个标志,然后在对象的setter方法中使该标志无效(如果该对象已更新),这样,如果您确实想要一个新的对象,就可以始终检查该标志.

A final point to think carefully about - you're returning the same object each time which has implications. If, eg., one piece of code retrieves an object and updates a value but doesn't commit that change to the db, all other code retrieving that object will see that change. This can be very useful if you're stringing together a series of operations - they can all update the object and then you can commit it at the end - but it may not be what you intend. I usually set a flag on the object when it is fresh from the database and then in your setter method invalidate that flag if the object is updated - that way you can always check that flag if you really want a fresh object.

这篇关于有没有办法为Class :: DBI缓存机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆