简体   繁体   English

有没有办法为Class :: DBI缓存机制?

[英]Is there a way to caching mechanism for Class::DBI?

I have a set of rather complex ORM modules that inherit from Class::DBI . 我有一组从Class :: DBI继承的相当复杂的ORM模块。 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. 我找到了一个模块: Class :: DBI :: Cacheable但没有评级或RT的任何评论。 I would appreciate hearing from people who have used this or any other Class::DBI caching scheme. 我希望听到使用过这个或任何其他Class :: DBI缓存方案的人的意见。

Thanks a ton. 万分感谢。

I too have rolled my own ORM plenty of times I hate to say! 我也不厌其烦地说过我自己的ORM很多次! Caching/Memoization is pretty easy if all your fetches happen through a single api (or subclasses thereof). 如果所有提取都通过单个api(或其子类)发生,则缓存/记忆很容易。

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
}

Instead of a hash, you can use the Cache:: suite of modules on CPAN to implement time and memory limits in your cache. 您可以使用CPAN上的Cache :: suite模块来实现缓存中的时间和内存限制,而不是哈希。

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. 例如,如果所有更新都通过ORM,则可以清除(或更新)update()ORM方法中的缓存条目。

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. 例如,如果一段代码检索一个对象并更新一个值但没有将该更改提交给db,那么检索该对象的所有其他代码都将看到该更改。 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. 我通常在数据库中新鲜时在对象上设置一个标志,然后在setter方法中如果对象更新则使该标志无效 - 这样,如果你真的想要一个新对象,你总是可以检查该标志。

On a few occasions we've rolled our own, but we limited it to special cases where profiling indicated we needed a boost (for example large joins). 有几次我们已经推出了自己的产品,但我们将其限制在特殊情况下,其中分析表明我们需要提升(例如大型连接)。 Since our applications typically use a custom abstraction layer (akin to a home-grown ORM) on top of the DB access, that's where we implemented the caching. 由于我们的应用程序通常在数据库访问之上使用自定义抽象层(类似于自行开发的ORM),因此我们实现了缓存。 We achieved good results that we were satisfied with and it didn't take a whole lot of effort. 我们取得了令人满意的良好成果,并没有花费太多精力。 Of course, since we weren't using a CPAN ORM, we didn't really have any choice about using a CPAN caching module, either. 当然,由于我们没有使用CPAN ORM,我们也没有选择使用CPAN缓存模块。

It was strictly case-by-case and opt-in. 这是严格的个案和选择。 Whether you end up using a CPAN solution or rolling your own, it's probably a good idea to restrict it to cases where profiling indicates you need help, and make sure that it's opt-in so your caching doesn't undermine your application in subtle ways by being active when you didn't expect it. 无论您最终使用的是CPAN解决方案还是自己编写解决方案,最好将其限制在分析表明您需要帮助的情况下,并确保选择加入,以便您的缓存不会以微妙的方式破坏您的应用程序当你没想到的时候通过活跃。

我之前使用过memcached缓存对象 ,但是没有使用Class :: DBI(ORM让我觉得很脏)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM