Skip to content

Instantly share code, notes, and snippets.

@Crell
Last active August 29, 2015 14:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Crell/12f8331dda65ea303ddd to your computer and use it in GitHub Desktop.
Expiry cache extension
<?php
class AccessedPolicy implements PolicyInterface {
public function __construct($access_count) { /*...*/ }
}
<?php
interface ExpireableItemInterface extends Psr\Cache\CacheItemInterface
{
public function setExpiryPolicy(PolicyInterface);
}
<?php
interface ExpireablePoolInterface extends Psr\Cache\CachePoolInterface
{
// .. Whatever.
}
<?php
interface PolicyInterface {
// ... Whatever.
}
<?php
/**
* Caches a widget.
*/
function set_widget(ExpireablePoolInterface $pool, Widget $widget)
{
$key = 'widget.' . $widget->id();
$item = $pool->getItem($key);
// Expire this item after 5 reads or 300 seconds (5 minutes), whichever comes first.
$item->setExpiryPolicy(new AccessedPolicy(5));
$item->expireAfter(300);
$item->set($widget);
$pool->save($item);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment