snakano/propel-data-cache-behavior

A Propel ORM behavior that provide auto data caching to your model.

1.2.2 2016-01-12 04:03 UTC

This package is auto-updated.

Last update: 2024-02-25 19:30:01 UTC


README

Build Status Latest Stable Version Total Downloads

A Propel ORM behavior that provide auto data caching to your model.

  • support caching system APC, memcached and Redis (via DominoCacheStore)
  • auto caching and auto flush.

What's the difference Query Cache Behavior

Query Cache Behavior is caching transformation of a Query object (caching SQL code).
This Behavior is caching the results of database. (result data cache)

Requirements

Install

Composer

Add a dependency on snakano/propel-data-cache-behavior to your project's composer.json file.

{
    "require": {
        "snakano/propel-data-cache-behavior": "1.*"
    }
}

Then, add the following configuration to your build.properties or propel.ini file:

propel.behavior.data_cache.class = lib.vendor.snakano.propel-data-cache-behavior.src.DataCacheBehavior

Configuration

schema.xml

<table name="book">
  <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
  <column name="title" type="VARCHAR" required="true" primaryString="true" />
  <behavior name="data_cache">
    <parameter name="auto_cache" value="true" /> <!-- auto cache enable. default true (optional) -->
    <parameter name="backend" value="apc" />     <!-- cache system. "apc" or "memcache", default "apc". (optional) -->
    <parameter name="lifetime" value="3600" />   <!-- cache expire time (second). default 3600 (optional) -->
  </behavior>
</table>

if use memcached.

Add the following configuration code to your project bootstraping file.

// configure memcached setting.
Domino\CacheStore\Factory::setOption(
    array(
        'storage'     => 'memcached',
        'prefix'      => 'domino_test',
        'default_ttl' => 360,
        'servers'     => array(
            array('server1', 11211, 20),
            array('server2', 11211, 80)
        )
    )
);

Basic usage

$title = 'War And Peace';
BookQuery::create()
    ->filterByTitle($title)
    ->findOne(); // from Database

BookQuery::create()
    ->filterByTitle($title)
    ->findOne(); // from caching system

Disable cache

$title = 'Anna Karenina';
BookQuery::create()
    ->setCacheDisable()  // disable cache
    ->filterByTitle($title)
    ->findOne();
  • setCacheEnable()
  • setCacheDisable()
  • isCacheEnable()
  • setLifetime($ttl)

When cache delete?

$book = new Book;
$book->setId(1);
$book->setTitle("War And Peace");
$book->save();  // purge cache.
  • expire cache lifetime.
  • call save() method.
  • call delete() method.
  • call BookPeer::doDeleteAll() method.
  • call BookPeer::purgeCache() method.

Manually delete cache.

$title = 'War And Peace';
$query = BookQuery::create();
$book  = $query->filterByTitle($title)->findOne();
$cacheKey = $query->getCacheKey(); // get cache key.

BookPeer::cacheDelete($cacheKey);  // delete cache by key.

License

MIT License