DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Using sfFileCache directly

Using sfFileCache directly

Stefan Koopmanschap user avatar by
Stefan Koopmanschap
·
Feb. 19, 08 · Web Dev Zone · Interview
Like (0)
Save
Tweet
5.61K Views

Join the DZone community and get the full member experience.

Join For Free

You can configure a lot of caching in symfony, so rarely do you need to cache things yourself, but it may happen that one day you decide you need it. I came to that point when I wanted to cache certain results from external web services. I could have used the Function caching, but in this case I wanted to keep a bit more control, possibly altering the cache etc.

To do this I wrote a little wrapper class that contained three little methods, but that opened up the symfony file cache for my own usage. I wanted to ensure I didn't end up with tons of cache objects, so first I implemented a method that would only create a new cache object if I didn't have one already, or else just return the object:

public static function getCacheObject() {
if (!self::$cache instanceof sfFileCache) {
$file_cache_dir = sfConfig::get('sf_cache_dir') . '/my_own_filecache';
self::$cache = new sfFileCache($file_cache_dir);
}

return self::$cache;
}

as you see, the cache is being written in a directory called /my_own_filecache inside the symfony cache dir. I could save this everywhere of course, just decided this would be a good place. Note: if you do a symfony cc then your cache will also be cleared, so if you want this not to happen, place your cache directory outside of symfony's cache directory.

Then I need a way to set something in the cache:

public static function setToCache($namespace, $name, $value) {
$file_cache = leftTools::getCacheObject();
$file_cache->setLifeTime(3600);
$file_cache->set($name, $namespace, serialize($value));
}

I set the lifetime of the cached value hard to 3600 seconds, but this could of course be dynamic. In my case, this is enough. I serialize the value myself, as I used to have some trouble with objects being cached. I'm actually not sure if it's still needed, as there were more problems with the objects. I'll leave it in for now ;)

And of course, what you set, you'll want to get, so here is the getter:

public static function getFromCache($namespace, $name) {
$file_cache = leftTools::getCacheObject();
if ($file_cache->has($name, $namespace)) {
$cached = $file_cache->get($name, $namespace);
if (!empty($cached)) {
return unserialize($cached);
}
}
}

Well, this is pretty simple. I only return something if there is something to return, and I unserialize before returning.

Using the sfFileCache is pretty simple as you can see. Use it to your advantage.

Article originally posted on Left on the Web.

 

Cache (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • Java Hashtable, HashMap, ConcurrentHashMap: Performance Impact
  • How to Generate Fake Test Data
  • Don't Underestimate Documentation

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo