Last active
December 20, 2015 14:59
-
-
Save karptonite/6150975 to your computer and use it in GitHub Desktop.
Suggested implentation for Laravel Cache Sections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Intended use of Sections | |
//This is not functional code, but will give you and idea of the changes intended | |
//an ordered array of section names | |
$sections = array( 'people', 'authors' ); | |
//to store | |
Cache::sections( $sections )->put('John', $john); | |
//to access | |
//note that you need the whole list of sections to access | |
Cache::sections( $sections )->get('John'); | |
//Flushing either section will flush John's cache. | |
//to flush just the authors | |
Cache::section( 'authors' )->flush(); | |
//to flush all the people | |
Cache::section( 'people' )->flush(); | |
//to flush multiple sections | |
Cache::sections( $sections )->flush(); | |
//Implementation | |
//Note: I'm using the "Sections" terminology for the underlying class, | |
//but I think that Tags might be clearer, if you wanted to make that change. | |
//In MemcacheStore and other stores that use sections: | |
public function sections(Array $names) | |
{ | |
return new SectionedCache($this, $names); | |
} | |
public function section($name) | |
{ | |
return $this->sections(array($name)); | |
} | |
//the SectionedCache class will be adapted from the current Section.php, with the following changes | |
//the constructor will store $this->names instead of $this->name. | |
public function __construct(StoreInterface $store, $names) | |
{ | |
//sort($names); we could add this sort line here, if we don't want to require the list of names in order | |
$this->names = $names; | |
$this->store = $store; | |
} | |
// | |
//sectionId and sectionKey will take a $name parameter as follows. This might be better pulled into a separate class-- | |
//I just wanted to get the idea across for this example | |
protected function sectionId($name) | |
{ | |
$id = $this->store->get($this->sectionKey($name)); | |
if (is_null($id)) | |
{ | |
$id = $this->resetSection($name); | |
} | |
return $id; | |
} | |
public function resetSection($name) | |
{ | |
$this->store->forever($this->sectionKey($name), $id = uniqid()); | |
return $id; | |
} | |
protected function sectionKey($name) | |
{ | |
return 'section:'.$name.':key'; | |
} | |
//two functions (could be combined to one) return a namespace to add to a cache name. | |
// | |
protected function sectionIds() | |
{ | |
$ids = array(); | |
foreach ($this->names as $name) | |
$ids = $this->sectionId($name); | |
return $ids; | |
} | |
protected function getNamespace() | |
{ | |
//The sha1 ensures that the namespace is not too long, but is otherwise unnecessary | |
return sha1(implode('|', $this->sectionIds())); | |
} | |
protected function sectionItemKey($key) | |
{ | |
//Could also concatenate the names to the front--but again it could lead to overly-long key names | |
return $this->getNamespace() . ':' . $key; | |
} | |
//reset resets all sections | |
protected function reset() | |
{ | |
foreach($this->names as $name) | |
{ | |
$this->resetSection($name); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment