|
NAMECHI::Driver::Development - Manual for developing new CHI driversVERSIONversion 0.60SYNOPSISpackage CHI::Driver::MyDriver; use Moo; use strict; use warnings; extends 'CHI::Driver'; has ...; sub fetch { my ( $self, $key ) = @_; } sub store { my ( $self, $key, $data[, $expires_in] ) = @_; } sub remove { my ( $self, $key ) = @_; } sub clear { my ($self) = @_; } sub get_keys { my ($self) = @_; } sub get_namespaces { my ($self) = @_; } DESCRIPTIONThis document describes how to implement a new CHI driver.The easiest way to start is to look at existing drivers, such as CHI::Driver::Memory and CHI::Driver::FastMmap. NAMINGIf you are going to publicly release your driver, call it 'CHI::Driver::something' so that users can create it withCHI->new(driver => 'I<something>'); If it's an internal driver, you can call it whatever you like and create it like CHI->new(driver => '+My::Internal::CHI::Driver'); MOO / MOOSECHI driver classes must be Moo or Moose based to be fully functional, since we use Moose roles to implement various features. For backward compatibility, non-Moo/Moose drivers will still work at a basic level, but you will see an error if using any feature requiring a role.All drivers must directly or indirectly extend CHI::Driver. NAMESPACEAll cache handles have an assigned namespace that you can access with "$self->namespace". You should use the namespace to partition your data store. That is, two cache objects with different namespaces should be able to access the same key without any collision.Examples:
METHODSRequired methodsThe following methods have no default implementation, and MUST be defined by your subclass:
Overridable methodsThe following methods have a default implementation, but MAY be overridden by your subclass:
Optional methodsThe following methods have no default implementation, and MAY be defined by your subclass, but are not required for basic cache operations.
DISCARD POLICIESYou can create new discard policies for size aware caches, to choose items to discard when the cache gets full. For example, the Memory driver implements an LRU policy.To implement a discard policy foo, define a subroutine discard_policy_foo, which takes a driver object and returns a closure that returns one key each time it is called. The closure should maintain state so that each key is only returned once. For example, here's the Memory driver's LRU implementation. It utilizes a hash containing the last used time for each key. sub discard_policy_lru { my ($self) = @_; my $last_used_time = $self->{metadata_for_namespace}->{last_used_time}; my @keys_in_lru_order = sort { $last_used_time->{$a} <=> $last_used_time->{$b} } $self->get_keys; return sub { shift(@keys_in_lru_order); }; } You can set the default discard policy for your driver by overriding default_discard_policy; otherwise the default is 'arbitrary'. sub default_discard_policy { 'lru' } TESTINGCHI has a standard set of unit tests that should be used to ensure your driver is fully implementing the CHI API.To use CHI's tests (replacing MyDriver with the name of your driver):
Test cleanupYou are responsible for cleaning up your datastore after tests are done. The easiest way to do this is to place your datastore wholly inside a temporary directory, or use a guard to remove it at process end.For example, the File, FastMmap, and DBI tests place all data inside a tempdir that is automatically cleaned up at process end. SEE ALSOCHIAUTHORJonathan Swartz <swartz@pobox.com>COPYRIGHT AND LICENSEThis software is copyright (c) 2012 by Jonathan Swartz.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Visit the GSP FreeBSD Man Page Interface. |