Developed with love by KnpLabs Hire us for your project!
48

CacheBundle

by beryllium

Symfony2 bundle for memcache and other caching interfaces

BerylliumCacheBundle for Symfony2

It's memcache. You've seen it before. Now it's injectable to the DIC, and you don't have to write all this junk yourself. And it should also work with Amazon ElasticCache, as well as the MySQL Memcache Interface (new in MySQL 5.6).

Another advantage over just using a raw memcache object is that BCB probes servers before adding them to the pool - if a server is down, your site won't be gravely impacted by the Memcache class waiting and waiting and waiting for a response.

The groundwork is also laid out for building alternate cache interfaces quickly - such as APC caching, or your own home-rolled filesystem cache.

Configuration

Step 1: Fetching

If you are using composer, you probably only need to add this to your composer.json file:

"require": {
  "Beryllium/CacheBundle": "dev-master"
},

If you aren't using composer, add this to your deps file:

[BerylliumCacheBundle]
    git=http://github.com/beryllium/CacheBundle.git
    target=/bundles/Beryllium/CacheBundle

And then run the update vendors script:

bin/vendors install

Step 2: Configure autoload.php

If you aren't using Composer, register the namespace like so:

# app/autoload.php

<?php

$loader->registerNamespaces( array(
  //...
  'Beryllium' => __DIR__.'/../vendor/bundles',
  ) );

Step 3: Configure the AppKernel

Add it to your AppKernel:

# app/AppKernel.php

<?php

    $bundles = array(
        //...
        new Beryllium\CacheBundle\BerylliumCacheBundle(),
    );

Configure your server list in parameters.ini:

beryllium_cache.client.servers["127.0.0.1"] = 11211

Or for parameters.yml:

parameters:
    ...
    beryllium_cache.client.servers: { "localhost": 11211 }

If you want to change the default cache TTL value (300 seconds) you can
add this to your parameters.yml:

parameters:
    ...
    beryllium_cache.default_ttl: 86400

If you plan on using local UNIX sockets, GitHub user gierschv has contributed the ability to do this:

beryllium_cache.client.servers["unix:///tmp/mc.sock"]=""

And then you should be good to go:

$this->get( 'beryllium_cache' )->set( 'key', 'value', $ttl );
$this->get( 'beryllium_cache' )->get( 'key' );

You might want to set up a service alias, since "$this->get( 'beryllium_cache' )" might be a bit long.

The Command Line

For a command line report of CacheClient statistics (assuming the cache client has a ->getStats method, which is not an interface requirement), you can do the following:

app/console cacheclient:stats

Example Output:

Servers found: 1

Host: 127.0.0.1:11211
Usage: 0% (0.01MB of 64MB)
Uptime: 344976 seconds (3 days, 23 hours, 49 minutes, 36 seconds)
Open Connections: 15
Hits: 26
Misses: 29
Helpfulness: 47.27%

Or, for extended information (the raw stats array), you can run with debugging enabled:

app/console cacheclient:stats --debug

Help is available, although brief:

app/console help cacheclient:stats

The Future

Currently there aren't any unit or functional tests. So that needs to be worked on.

More cache client implementations could be useful, if it turns out there's a demand for them.

And yes, the documentation needs to be more thorough as well. I've made some improvements, but it's still spotty at best.

Beyond that, who knows what the future might hold.

Additional Resources

MySQL InnoDB+Memcached API:

Amazon ElastiCache:

Copyright (c) 2011 Kevin Boyd (aka "Beryllium")

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
beryllium_cache:      []
  • Merge pull request #18 from gezpage/master
    By jeremylivingston, 1 year ago
  • Allow default TTL to be set via beryllium_cache.default_ttl parameter
    By gezpage, 1 year ago
  • Merge pull request #13 from ebaioni/master
    By jeremylivingston, 1 year ago
  • Add prefix functionality for Memcache keys
    By jeremylivingston, 1 year ago
  • made prefix as a setter dependency
    By ebaioni, 1 year ago
  • string prefix for keys added
    By ebaioni, 1 year ago
  • Merge pull request #10 from beryllium/formatting
    By jeremylivingston, 1 year ago
  • Update formatting to comply with Symfony2 standards
    By jeremylivingston, 1 year ago
  • Merge pull request #9 from jeremylivingston/fsock-warning
    By beryllium, 1 year ago
  • Add suppression flag to fsockopen call to quiet unnecessary warnings.
    By jeremylivingston, 1 year ago
  • Adding some info for Symfony 2.1 users
    By beryllium, 1 year ago
  • Adding delete method per issues #3 and #6 - can only handle one key at a time at the moment
    By beryllium, 2 years ago
  • Fixing markdown glitches
    By Kevin Boyd, 2 years ago
  • Updating the readme a bit
    By Kevin Boyd, 2 years ago
  • Minor updates
    By Kevin Boyd, 2 years ago
  • Merge pull request #5 from gierschv/master
    By beryllium, 2 years ago
  • Fix UNIX Socket add : use intval instead of is_int()
    By Vincent Giersch, 2 years ago
  • Add UNIX memcached socket support
    By Vincent Giersch, 2 years ago
  • Merge pull request #4 from gierschv/master
    By beryllium, 2 years ago
  • Fix division by zero when there is no hit
    By gierschv, 2 years ago
  • Streamlining the memcache client constructor and configuration, to make way for easier unit testing
    By Kevin Boyd, 2 years ago
  • Merge branch 'master' of github.com:beryllium/CacheBundle
    By Kevin Boyd, 2 years ago
  • Adding a composer.json file
    By Kevin Boyd, 2 years ago
  • Update LICENSE
    By beryllium, 2 years ago
  • Updating Readme with CLI output example
    By beryllium, 2 years ago
  • Update README.md
    By beryllium, 2 years ago
  • Fixing an improper line of documentation
    By Kevin Boyd, 2 years ago
  • Formatting fixes
    By Kevin Boyd, 2 years ago
  • Formatting fixes
    By Kevin Boyd, 2 years ago
  • Updating documentation a bit
    By Kevin Boyd, 2 years ago