Is there a way to cache the mechanism for the :: DBI class?

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

Thanks a ton.

+3
source share
3 answers

I also wrapped my ORM many times, I hate talking! Caching / Storing is pretty simple if all of your selections happen through one api (or their subclasses).

For any selection based on a unique key, you can simply cache based on key concatenation. A naive approach could 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 in CPAN to implement time and memory restrictions in your cache.

If you intend to cache for some time, you may need to think about the possibility of expiration of objects in the cache. If, for example, all of your updates also go through ORM, you can clear (or update) the cache entry in the update ORM () method.

, , - , . , , , db, , , . , - , , , . , , setter , . , .

+3

, , , (, ). ( ORM) , , . , , . , ORAN CPAN, CPAN.

. CPAN , , , , , , , , .

+2

I used memcached before cache objects , but not with Class :: DBI (ORM makes me feel dirty).

0
source

Source: https://habr.com/ru/post/1706541/


All Articles