CHI - YAPC NA 2012

1,449 views

Published on

Published in: Technology
  • Be the first to comment

CHI - YAPC NA 2012

  1. 1. CHI: Universal caching for Perl Jonathan Swartz YAPC::NA 2012 Madison, WI
  2. 2. What is CHI?• Cache Handling Interface• Standard interface and implementation for Perl caching ... like DBI for caching• Caching = “Get and set key/values with expiration”
  3. 3. We already have...
  4. 4. What’s it good for?• Implement standard caching features in a single place • Namespaces • Logging • Serialization • Statistics collection • Multilevel caches • Error handling • Probablistic expiration • Compression • Max size and discard • Configuration• Make new cache backends trivial to create
  5. 5. Who needs caches?• Web frameworks (Catalyst, Dancer, ...)• Sessions (Plack::Session, CGI::Session, ...)• ORMs (DBIx::Class, RDBO, ...)• Templating (TT, Mason, ...)• Code processing (Perl::Tidy, Perl::Critic, ...)• Slow to startup (Class::MOP/Moose, ...)
  6. 6. Using CHI
  7. 7. Namespaces• Separate cache keys into silos• Can be inspected, configured, and cleared independently• Typically package or template name
  8. 8. Drivers• Memory/RawMemory - In-process memory• File - Hierarchical, one file per entry• FastMmap - Mmaped files• BerkeleyDB• DBI• Memcached, Redis, TokyoTyrant - Distributed• ...
  9. 9. Probabilistic expiration• Good for avoiding “miss stampedes”
  10. 10. L1/L2 cache• Store sessions in mysql, fronted by memcached
  11. 11. Mirror cache• Migrate cache from /old/root to /new/root
  12. 12. CHI::Memoize• Like Memoize, only CHI-ier
  13. 13. Why is compute() better?• Less typing• Background recomputation * (return old value immediately, recompute in background)• Can measure compute time and thus cache utility * - theoretical, not yet implemented
  14. 14. Cache utilityHR = hit rateCget = cost of getCset = cost of setCcompute = cost of compute Cget + (Ccompute + Cset) / HRUtility = ------------------------------- Ccompute
  15. 15. Stats
  16. 16. Stats collected per namespace• # hits, misses, and sets hit rate• avg cost of get, set, and compute (ms)• avg key and value size
  17. 17. Resources• Get CHI from CPAN - http://search.cpan.org/perldoc?CHI• Search for drivers - http://cpan.uwinnipeg.ca/search?query=CHI::Driver• Subscribe to perl-cache mailing list - http://groups.google.com/group/perl-cache-discuss
  18. 18. BenchmarksDescription Get time Set timeCHI::Driver::MemoryRaw 0.019ms 0.036msCHI::Driver::Memory 0.042ms 0.066msCHI::Driver::BerkeleyDB 0.052ms 0.073msCHI::Driver::FastMmap 0.057ms 0.087msCHI::Driver::DBI (sqlite) 0.112ms 1.659msCHI::Driver::File 0.118ms 1.138msCHI::Driver::Memcached::Fast 0.138ms 0.178msCHI::Driver::Memcached::libm 0.151ms 0.202msCHI::Driver::DBI (mysql) 0.236ms 0.273msCHI::Driver::Memcached 0.279ms 0.235ms
  19. 19. Creating a driver
  20. 20. Creating a cachemy $cache = CHI->new( driver => File, namespace => ‘Foo::Bar’, cache_root => /path/to/root);my $cache = CHI->new( driver => FastMmap, root_dir => /path/to/root, cache_size => 1k);my $cache = CHI->new( driver => Memcached, servers => ["10.0.0.15:11211”, ...]);

×