代码之家  ›  专栏  ›  技术社区  ›  Julien

DBIx::类是否具有透明缓存?

  •  9
  • Julien  · 技术社区  · 17 年前

    在C#中。在网络世界中,有一些ORM,如NHibernate或ActiveRecord,包括透明缓存:数据库更新透明地复制到缓存中,对象在可用时直接从缓存中检索,等等(通常使用memcached)。

    DBIx::Class

    4 回复  |  直到 17 年前
        1
  •  6
  •   MkV    17 年前

        2
  •  5
  •   friedo    17 年前

    CHI 。我实际上还没有尝试过,所以可能有我没有考虑过的陷阱,特别是在DBIC结果集的序列化方面。

    package My::Table;
    use strict; 
    use warnings;
    
    use base 'DBIx::Class';
    
    use Storable 'freeze';
    use CHI;
    
    $Storable::canonical = 1;
    
    __PACKAGE__->load_components(qw/Core/);
    __PACKAGE__->table('mytable');
    
    # ....
    
    my $CACHE = CHI->new( driver => 'Memory' );
    
    sub search { 
        my $self = shift;
    
        my $key = freeze( \@_ );      # make cache key from params
        if ( my $rs = $CACHE->get( $key ) ) { 
            return $rs;
        }
    
        # Note: there are issues with context propagation here
        my $rs = $self->next::method( @_ );
        $CACHE->set( $key => $rs );
        return $rs;
    }
    
    sub update { 
        my $self = shift;
    
        my @keys = $self->find_all_cache_items_affected_by_this_update( @_ );
        $CACHE->remove( $_ ) for @keys;
    
        $self->next::method( @_ );
    }
    

        3
  •  1
  •   redhotpenguin    15 年前

    例如,我目前使用理想缓存的代码如下:

    my $network = SL::Model::App->resultset('Network')->search({ ip => '127.0.0.1' });
    

    my $network = SL::Network->find_by_ip_or_create({ ip => '127.0.0.1' });
    

    同时,在附近的模块中:

    package SL::Network;
    ...
    use SL::Model::App;
    use SL::Cache;
    
    our $cache = SL::Cache->new;
    
    sub find_by_ip_or_create {
        my ($class, $args) = @_;
    
        my $network;
        unless ($network = $cache->get('network|' . $args->{ip}) {
            $network = SL::Model::App->resultset('Network')->find_or_create({ wan_ip => $args->{ip}});
            $cache->set('network|' . $args->{ip} => DBIx::Class::Schema->freeze($network));
        }
        return $network;
    
    }
    

        4
  •  0
  •   morkro Maksim Luzik    11 年前

    我想补充一下,而不是在中添加“搜索”方法 My::Table

    DBIx::Class::ResultSet

    package Schema::ResultSet::My::Table;
    use base 'DBIx::Class::ResultSet';
    
    sub search {
        my ( $self, $args ) = ( shift, shift );
    
        # do what you want here with the args passed to ->search
        return $self->next::method( $args, @_ );
    }
    

    "../Schema/ResultSet/" "load_namespaces();"

    DBIx::Class::ResultSet

    推荐文章