phy/cache-bundle

There is no license information available for the latest version (1.0.0) of this package.

Cache Bundle for Symfony2 that supports APC, Memcache, Memcached, Redis, Local, and Disk storage.

Installs: 8 489

Dependents: 0

Suggesters: 0

Security: 0

Stars: 7

Watchers: 3

Forks: 1

Open Issues: 0

Type:symfony-bundle

1.0.0 2015-01-27 17:28 UTC

This package is not auto-updated.

Last update: 2024-12-16 16:22:47 UTC


README

Simple enough to use cache wrapper for Symfony2. Right now supports APC, Local Disk, Memcache, and Memcached.

Install

To include this along with your Symfony2 project then you can just include this in your composer config:

"require": {
    "phy/cache-bundle": "1.0.0"
}

And update composer accordingly.

Configuration

Here's an example of our services.yml, you don't really need to change this since values come from parameters.yml

services:
  phy_cache.client:
    class: '%phy_cache.class%'
    arguments: [ '%phy_cache.settings%' ]
  phy_cache:
    class: PHY\CacheBundle\Cache
    arguments: [ '@phy_cache.client' ]
    calls:
      - [ setExpiration, [ '%phy_cache.expiration%' ] ]
      - [ setCompression, [ '%phy_cache.compression%' ] ]
      - [ setPrefix, [ '%phy_cache.prefix%' ] ]

Here's how you'd setup some common caches in your parameters.yml

Memcache

parameters:
    phy_cache.class: PHY\CacheBundle\Cache\Memcache
    phy_cache.settings:
      server: [ 127.0.0.1:11211 ]
    phy_cache.prefix: phy_
    phy_cache.expiration: 300
    phy_cache.compression: 0

Disk

parameters:
    phy_cache.class: PHY\CacheBundle\Cache\Disk
    phy_cache.settings:
      location: /var/tmp/cache/
    phy_cache.expiration: 300
    phy_cache.compression: 0

Apc

parameters:
    phy_cache.class: PHY\CacheBundle\Cache\Apc
    phy_cache.settings:
      mode: opcode
    phy_cache.prefix: phy_
    phy_cache.expiration: 300
    phy_cache.compression: 0

Redis

parameters:
    phy_cache.class: PHY\CacheBundle\Cache\Redis
    phy_cache.settings:
      server: 127.0.0.1
      port: 6379
      password: pants
    phy_cache.prefix: phy_
    phy_cache.expiration: 300
    phy_cache.compression: 0

Usage

To call the cache inside your controller (or any container aware class) you just need to call it by its service name.

$cache = $this->container->get('phy_cache');

From there, you can start setting, getting, replacing, incrementing, decrementing, and so forth.

$id = 1;
$cache = $this->container->get('phy_cache');
$hashKey = 'someModel/'.$id;

if(!$someModel = $cache->get($hashKey)) {
    $someModel = $this->loadModel($id);
    $cache->set($hashKey, $someModel, 300);
    $cache->increment($hashKey.'/loads', 1);
}

All methods available are almost 100% equal to \Memcache.

Command Line

There are several command line options to handle simple cache methods. All commands have an ability to overwrite the cache used with the --config option. If --config isn't set it will use the generic phy_cache otherwise you can pass along a config in a JSON string in this format:

{
  "type": "disk",
  "client": {
    "location": "./tmp"
  },
  "options": {
    "prefix": "phy_",
    "expiration": 300,
    "compression": 0
  }
}

Check current running stats (Needs some style and extra functionality)

php app/console phy:cache:stats

And flushing your cache completely

php app/console phy:cache:flush

Set something

php app/console phy:cache:set --key something --value 3 --compress 0 --expires 40

Replace something

php app/console phy:cache:replace --key something --value 3 --compress 0 --expires 40

Get something

php app/console phy:cache:get --key something --compress 0

Delete something

php app/console phy:cache:delete --key something

Decrement something

php app/console phy:cache:decrement --key something --value 3

About

Originally was using BerylliumCacheBundle by Kevin Boyd. Got the job done for the most part but I needed to compress some rather large aggregate results from MongoDB. Made a pull request to add the compression on and noticed certain other functions weren't quite implemented yet. Having already done a similar bundle for Cache (albeit not for Symfony2) figured I could just port that over and here we are.

Requirements

  • PHP 5.3+

Testing

Tests will try and use all the different cache clients and if they aren't installed, or cannot connect to the desired cache file then those tests will be skipped. To test Disk caching, there needs to be a tmp directory that is writable which will be cleaner before and after a test run. To run all you need to do in the command line is:

phpunit

Submitting bugs and feature requests

Please send bugs to me via GitHub

Author

John Mullanaphy - john@jo.mu - http://jo.mu/ That's it for now...

License

PHY/CacheBundle is licensed under the Open Software License (OSL 3.0) - see the LICENSE file for details

Acknowledgements