Swift's Runtime Cache

Swift Mailer incorporates a runtime cache to help speed things up a lot. Composing emails is a tough business. You need to encode files, correct line endings, translate to formats laid out in the RFCs. It’s therefore easier if you can just do this once and once it’s been done, re-use the results. Swift caches various pieces of data (computed strings) on various different levels whilst it composes messages. This is all handled by an abstracted caching layer.

The abstract cache is named Swift_Cache and contains methods write(), read(), readFull(), clear() and has(). All but readFull() are abstract.

By default, Swift will uses a Memory Cache, which essentially means it stores the encoded strings in variables which it can re-use. The alternative is to write this data to disk and save that memory. By writing the cache to disk, Swift should never use (much) more than about 1MB of memory, although this varies from system to system.

You can easily write your own cache by extending the Swift_Cache class and implementing the abstract methods. For examples, you could write a MySQL cache. If you do write your own cache, just bear in mind that write() is buffered (i.e. it appends to the cache, it doesn’t overwrite it) and read() is also buffered. You may choose to ignore the buffered aspect of read() - see the Swift_Cache_Memory class for an example.

Swift loads the cache by calling a factory named Swift_CacheFactory. It’s here that you can change the class which is instantiated in the factory.

Swift_CacheFactory::setClassName("Swift_Cache_MyCache");

If you want to use the Disk Cache (strongly recommended) then make sure you have a writable directory on disk and run the follwing just after you instantiate Swift.

$swift =& new Swift( ... );
Swift_CacheFactory::setClassName("Swift_Cache_Disk");
Swift_Cache_Disk::setSavePath("/tmp");

Changes made the the cache factory are global. Swift will only ever use one type of cache at a time.

 
v3/misc/caching.txt · Last modified: 2007/03/22 16:22 by d11wtq
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki