0

I am trying to adopt memcached in my project which is built under ZendFrame.

Firstly I create a Memcached class in the library(it seems a bit unecessary as it pretty much just encapsulates the functions what memcache offers)

Then I am wondering where exactly should I use it?

Controllers or the Mappers?

I can see both ways have its point, what's the conventional way for doing this.

Thanks, guys.

zhaopeng
  • 55
  • 7

1 Answers1

1

Under bootstrap.php you need to define the configuration using _initCache() function which returns void.

Define frontEnd driver, backend driver (where u will physically save data) and load the factory!

There are 2 types of caching, server and compression.

If you choose server, you need to define external extension (memcached is an extension)

A snippet which I would recommend is:

function _initCache() {

        $frontendDriver = 'Core';
        $frontendOptions = array(
            'lifetime' => 7200, // cache lifetime of 2 hours
            'automatic_serialization' => true
        );

        $backendDriver = extension_loaded('memcache') ? 'Memcached' : 'File';
        $backendOptions = array();

        // getting a Zend_Cache_Core object
        $cache = Zend_Cache::factory($frontendDriver, $backendDriver, $frontendOptions, $backendOptions);

        Zend_Registry::set('Zend_Cache', $cache);
    }

Getting data from Cache:

$date = $cache->load($cacheKey);

Saving data into cache:

$cache->save($data, $cacheKey);

Where can you use it?

ANYWHERE in your application!

Go through this good article.

Any Questions? :)

Keval Domadia
  • 4,768
  • 1
  • 37
  • 64
  • Thank you very much, Karmic Guru :-D This really expand my perception on this. Though one last question, yes, it's possible to be anywhere, but is there any convention or recommended rule that it should be only put in controllers or models, something like this? – zhaopeng Aug 17 '12 at 06:30
  • Guru? woah! Thanks ! :) It should be models. Models = business logic = fetching + manipulating + transactions from anything external (cache , database, etc are external sources). Now click on the tick-mark on the left hand side of my answer so that others can refer to it too ;) :D – Keval Domadia Aug 17 '12 at 06:33