function testSetCacheFilename_DestructorSaves() { $filename = tempnam('/tmp', 'foo'); // create some cache data. $cache = new Phlickr_Cache(); $cache->set('a key', 'value'); $cache->saveAs($filename); unset($cache); // load the cache and then delete the cache file $this->api->setCacheFilename($filename); unlink($filename); $this->assertFalse(file_exists($filename), 'the file should not exist'); // unsetting the api should call the destructor and writeout the file. unset($this->api); $this->assertTrue(file_exists($filename), 'the file should exist'); $cache = Phlickr_Cache::createFrom($filename); $this->assertType('Phlickr_Cache', $cache); $this->assertTrue($cache->has('a key'), 'value should exist'); }
/** * Set the name of the file used to save the cache when the object is * destroyed. * * If the file exists and is readable, an attempt will be made to load it * as a cache object using Phlickr_Cache::createFrom(). If the file does * not contain a valid cache object then a new, empty, cache object will * used and any previous cached information will be discarded. * * @param string The full path of the file. * @return void * @since 0.2.4 * @see __destruct(), getCacheFilename(), Phlickr_Cache::createFrom() */ public function setCacheFilename($filename) { $this->_cacheFilename = (string) $filename; if (file_exists($this->_cacheFilename)) { $this->_cache = Phlickr_Cache::createFrom($this->_cacheFilename); } }
function testCreateFrom_EmptyFile_AssignedShelfLife() { $tempName = tempnam('/tmp', 'cache'); touch($tempName); $this->assertTrue(file_exists($tempName)); $cache = Phlickr_Cache::createFrom($tempName, 999); unlink($tempName); $this->assertType('Phlickr_Cache', $cache); $this->assertEquals(999, $cache->getShelfLife()); $this->assertFalse($cache->has(self::URL_A)); $this->assertFalse($cache->has(self::URL_B)); }
/** * Get a random favorite photo from a Flickr user. * * @param string $userEmail Email address of a Flickr user * @return object Phlickr_Photo */ function getRandomFavoritePhoto($userEmail) { $api = new Phlickr_Api(FLICKR_API_KEY, FLICKR_API_SECRET); // load a saved cache file if it exists, set the expiration limit to a week. $api->setCache(Phlickr_Cache::createFrom(CACHE_FILE, 60 * 60 * 24 * 7)); // select a random favorite photo $user = Phlickr_User::findByEmail($api, $userEmail); $favlist = $user->getFavoritePhotoList(); $photo = $favlist->getRandomPhoto(); assert(!is_null($photo)); // serialize and save the cache file $api->getCache()->saveAs(CACHE_FILE); return $photo; }