Framework  3.9
FileBackedCache Class Reference

Provides a simple File-backed caching implementation for when no shared memory caching extension is present, but the application has access to a writeable folder. More...

Public Member Functions

 FileBackedCache ()
 
 keyToFile ($key)
 
 get ($key)
 
 put ($key, $obj, $ttl=0)
 
 invalidate ($key)
 
 invalidateMatching ($pattern)
 
 clear ()
 

Public Attributes

 $cache
 
 $cacheDir
 

Detailed Description

Provides a simple File-backed caching implementation for when no shared memory caching extension is present, but the application has access to a writeable folder.

This is NOT recommended for high-volume sites, but may be your only option on a shared server.

Definition at line 444 of file cache.inc.

Member Function Documentation

◆ clear()

FileBackedCache::clear ( )

Definition at line 526 of file cache.inc.

527  {
528  foreach(glob($this->cacheDir . DIRECTORY_SEPARATOR . "*.txt") as $doomed)
529  {
530  unlink($doomed);
531  }
532  }

◆ FileBackedCache()

FileBackedCache::FileBackedCache ( )

Definition at line 449 of file cache.inc.

450  {
451  global $config;
452 
453  trace("Creating File-backed Cache", 4);
454 
455  $this->cacheDir = $config["file_backed_cache_directory"];
456  $this->cache = array();
457 
458  trace("Cache Directory: {$this->cacheDir}", 3);
459  }
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010

◆ get()

FileBackedCache::get (   $key)

Definition at line 466 of file cache.inc.

467  {
468  if (array_key_exists($key, $this->cache)) return $this->cache[$key];
469  $file = $this->keyToFile($key);
470  if (file_exists($file))
471  {
472  $c = unserialize(file_get_contents($file));
473  $this->cache[$key] = $c;
474  return $c;
475  }
476  else return null;
477  }
keyToFile($key)
Definition: cache.inc:461

◆ invalidate()

FileBackedCache::invalidate (   $key)

Definition at line 512 of file cache.inc.

513  {
514  $file = $this->keyToFile($key);
515 
516  if (file_exists($file)) unlink($file);
517  unset($this->cache[$key]);
518  return true;
519  }

◆ invalidateMatching()

FileBackedCache::invalidateMatching (   $pattern)

Definition at line 521 of file cache.inc.

522  {
523  throw new FakoliException("FileBackedCache currently does not support the invalidateMatching() method");
524  }

◆ keyToFile()

FileBackedCache::keyToFile (   $key)

Definition at line 461 of file cache.inc.

462  {
463  return $this->cacheDir . DIRECTORY_SEPARATOR . $key . ".txt";
464  }

◆ put()

FileBackedCache::put (   $key,
  $obj,
  $ttl = 0 
)

Definition at line 479 of file cache.inc.

480  {
481  $this->cache[$key] = $obj;
482 
483  $file = $this->keyToFile($key);
484 
485  if (!file_exists($file))
486  {
487  $fp = fopen($file, "w");
488  fwrite($fp, serialize($obj));
489  fclose($fp);
490  }
491  else
492  {
493  $fp = fopen($file, "w+");
494 
495  if (flock($fp, LOCK_EX))
496  {
497  ftruncate($fp, 0);
498  fwrite($fp, serialize($obj));
499  flock($fp, LOCK_UN);
500  }
501  else
502  {
503  echo "Couldn't get the lock!";
504  }
505  }
506  fclose($fp);
507 
508  return true;
509  // NOTE - ttl is ignored if using in-memory caching.
510  }

Member Data Documentation

◆ $cache

FileBackedCache::$cache

Definition at line 446 of file cache.inc.

◆ $cacheDir

FileBackedCache::$cacheDir

Definition at line 447 of file cache.inc.


The documentation for this class was generated from the following file: