Skip to content

Commit

Permalink
Support synthetic events: default configuration just logs cache misses.
Browse files Browse the repository at this point in the history
  • Loading branch information
fgm committed Nov 27, 2013
1 parent 7ad101e commit d54d2e1
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 52 deletions.
1 change: 1 addition & 0 deletions classloader.inc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function heisencache_require() {
}

// Load cache-related classes.
require_once "$srcDir/EventSourceInterface.php";
require_once "$srcDir/EventSubscriberInterface.php";
require_once "$srcDir/BaseEventSubscriber.php";
require_once "$srcDir/EventEmitter.php";
Expand Down
23 changes: 17 additions & 6 deletions default.settings.heisencache.inc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@
// Operate in the plugin namespace
namespace OSInet\Heisencache;

// Obtain the plugin configuration instance.
$config = Config::instance();
$emitter = Config::instance()->getEmitter();

$config->getEmitter()
// ->register(new DebugSubscriber(array('beforeGetMultiple', 'onCacheConstruct')))
// ->register(new MissSubscriber())
->register(new WatchdogWriterSubscriber());
$emitter
->register(new MissSubscriber($emitter))

// MissSubscriber is also an event source, like Cache, so we can use its events.

// Sample configuration listening to everything. > insert 1 Mb per page.
// ->register(new WatchdogWriterSubscriber(array_merge(
// Cache::getEmittedEvents(),
// MissSubscriber::getEmittedEvents()
// )));

// Sample configuration to just record cache misses.
->register(new WatchdogWriterSubscriber(array_merge(
array('onShutdown'),
MissSubscriber::getEmittedEvents())
));
10 changes: 5 additions & 5 deletions src/Heisencache/BaseEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@

abstract class BaseEventSubscriber implements EventSubscriberInterface {

protected $events = array();
protected $subscribedEvents = array();

public function addEvent($eventName) {
$this->events[$eventName] = TRUE;
$this->subscribedEvents[$eventName] = TRUE;
}

public function getEvents() {
return array_keys($this->events);
public function getSubscribedEvents() {
return array_keys($this->subscribedEvents);
}

public function removeEvent($eventName) {
unset($this->events[$eventName]);
unset($this->subscribedEvents[$eventName]);
}
}
4 changes: 2 additions & 2 deletions src/Heisencache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* @package OSInet\Heisencache
*/
class Cache implements \DrupalCacheInterface {
class Cache implements \DrupalCacheInterface, EventSourceInterface {
/**
* @var
*/
Expand Down Expand Up @@ -57,7 +57,7 @@ public function __construct($bin) {
* @return \string[]
* The array of available events.
*/
public static function getEvents() {
public static function getEmittedEvents() {
if (!isset(static::$events)) {
$methods = get_class_methods("DrupalCacheInterface");
$events = array('onCacheConstruct', 'onShutdown');
Expand Down
10 changes: 9 additions & 1 deletion src/Heisencache/DebugSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function show() {

public function __construct(array $events = NULL) {
if (!isset($events)) {
$events = Cache::getEvents();
$events = array_merge(Cache::getEmittedEvents(), MissSubscriber::getEmittedEvents());
}
foreach ($events as $eventName) {
$this->addEvent($eventName);
Expand Down Expand Up @@ -71,6 +71,14 @@ public function afterIsEmpty($channel) {
$this->show();
}

public function miss($channel) {
$this->show();
}

public function missMultiple($channel) {
$this->show();
}

public function onCacheConstruct($channel) {
$this->show($channel);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Heisencache/EventEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function emit($eventName, $channel) {
* @return \OSInet\Heisencache\EventEmitter
*/
public function register(EventSubscriberInterface $subscriber) {
foreach ($subscriber->getEvents() as $eventName) {
foreach ($subscriber->getSubscribedEvents() as $eventName) {
$this->on($eventName, $subscriber);
}
return $this;
Expand Down
21 changes: 21 additions & 0 deletions src/Heisencache/EventSourceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* @file
* EventSourceInterface.php
*
* @author: marand
*
* @copyright (c) 2013 Ouest Systèmes Informatiques (OSInet).
*
* @license General Public License version 2 or later
*/

namespace OSInet\Heisencache;


interface EventSourceInterface {
/**
* @return string[]
*/
public static function getEmittedEvents();
}
2 changes: 1 addition & 1 deletion src/Heisencache/EventSubscriberInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function addEvent($eventName);
/**
* @return string[]
*/
public function getEvents();
public function getSubscribedEvents();

/**
* @param string $eventName
Expand Down
57 changes: 47 additions & 10 deletions src/Heisencache/MissSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,95 @@
namespace OSInet\Heisencache;


class MissSubscriber extends BaseEventSubscriber {
class MissSubscriber extends BaseEventSubscriber implements EventSourceInterface {

const NAME = "misses";

protected $events = array(
/**
* @var \OSInet\Heisencache\EventEmitter
*/
protected $emitter;

protected $subscribedEvents = array(
'afterGet' => 1,
'afterGetMultiple' => 1,
'beforeGetMultiple' => 1,
);

protected static $emittedEvents = array(
'miss',
'missMultiple',
);

protected $multipleCids = array();

public function __construct(EventEmitter $emitter) {
$this->emitter = $emitter;
}

/**
* @param string $channel
* @param string $cid
* @param mixed $value
*
* @return array
*/
public function afterGet($channel, $cid, $value) {
if ($value === FALSE) {
$ret = array(
$missInfo = array(
'subscriber' => static::NAME,
'op' => 'get',
'bin' => $channel,
'requested' => array($cid),
'misses' => array($cid),
);

$this->emitter->emit('miss', $channel, $missInfo);
}
else {
$ret = NULL;
$missInfo = array();
}

$ret = serialize($ret);
echo "<p>" . __METHOD__ . ": $ret\n";
return $missInfo;
}

/**
* @param string $channel
* @param string[] $missed_cids
*
* @return array
*/
public function afterGetMultiple($channel, $missed_cids) {
$requested = $this->multipleCids;
$this->multipleCids = array();

if (!empty($missed_cids)) {
$ret = array(
$missInfo = array(
'subscriber' => static::NAME,
'op' => 'get_multiple',
'bin' => $channel,
'requested' => $requested,
'misses' => $missed_cids,
);
$missInfo['full_miss'] = ($missed_cids == $requested);

$this->emitter->emit('missMultiple', $channel, $missInfo);
}
else {
$ret = NULL;
$missInfo = array();
}

$ret = serialize($ret);
echo "<p>" . __METHOD__ . ": $ret\n";
return $missInfo;
}

public function beforeGetMultiple($channel, $cids) {
$this->multipleCids = $cids;
}

/**
* {@inheritdoc}
*/
public static function getEmittedEvents() {
return static::$emittedEvents;
}
}
12 changes: 7 additions & 5 deletions src/Heisencache/WatchdogWriterSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class WatchdogWriterSubscriber extends BaseEventSubscriber {

public function __construct(array $events = NULL) {
if (!isset($events)) {
$events = Cache::getEvents();
$events = Cache::getEmittedEvents();
}
foreach ($events as $eventName) {
$this->addEvent($eventName);
Expand Down Expand Up @@ -48,7 +48,7 @@ public function genericCall($eventName, $args) {
* @param $name
*/
public function __call($eventName, $args) {
if (!in_array($eventName, $this->events)) {
if (!in_array($eventName, $this->subscribedEvents)) {
throw new \InvalidArgumentException("Unsupported event $eventName");
}
else {
Expand All @@ -57,8 +57,10 @@ public function __call($eventName, $args) {
}

public function onShutdown($channel) {
watchdog('heisencache', 'Cache events: @events', array(
'@events' => serialize($this->history),
), WATCHDOG_DEBUG);
if (!empty($this->history)) {
watchdog('heisencache', 'Cache events: @events', array(
'@events' => serialize($this->history),
), WATCHDOG_DEBUG);
}
}
}
14 changes: 9 additions & 5 deletions src/Heisencache/tests/DebugSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OSInet\Heisencache\Cache;
use OSInet\Heisencache\DebugSubscriber;
use OSInet\Heisencache\EventEmitter;
use OSInet\Heisencache\MissSubscriber;

class DebugSubscriberTest extends \PHPUnit_Framework_TestCase {
/**
Expand All @@ -26,7 +27,7 @@ class DebugSubscriberTest extends \PHPUnit_Framework_TestCase {
protected $events = NULL;

public function setUp() {
$this->events = Cache::getEvents();
$this->events = array_merge(Cache::getEmittedEvents(), MissSubscriber::getEmittedEvents());
}

public function testExplicitEventRegistration() {
Expand All @@ -35,25 +36,25 @@ public function testExplicitEventRegistration() {
$events = array($event1, $event2);

$sub = new DebugSubscriber($events);
$actual = $sub->getEvents();
$actual = $sub->getSubscribedEvents();
$this->assertEquals($actual, $events);
}

public function testImplicitEventRegistration() {
$sub = new DebugSubscriber();
$actual = $sub->getEvents();
$actual = $sub->getSubscribedEvents();
$this->assertEquals($actual, $this->events);
}

public function testEventHandling() {
$channel = "somebin";
$mock = $this->getMockBuilder(self::FQCN)
->setMethods(array('getEvents', 'show'))
->setMethods(array('getSubscribedEvents', 'show'))
->getMock();
$mock->expects($this->exactly(count($this->events)))
->method('show');
$mock->expects($this->once())
->method('getEvents')
->method('getSubscribedEvents')
->will($this->returnValue($this->events));

$emitter = new EventEmitter();
Expand All @@ -77,6 +78,9 @@ public function testEventHandling() {

'beforeIsEmpty' => array(),
'afterIsEmpty' => array(),

'miss' => array(),
'missMultiple' => array(),
);

foreach ($eventMap as $eventName => $eventArgs) {
Expand Down
4 changes: 2 additions & 2 deletions src/Heisencache/tests/EventEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ public function testRegister() {
$event1 = 'event1';
$event2 = 'event2';
$events = array($event1, $event2);
$mocked = array_merge($events, array('getEvents'));
$mocked = array_merge($events, array('getSubscribedEvents'));

$subscriber = $this->getMockSubscriber($mocked);
$subscriber->expects($this->once())
->method('getEvents')
->method('getSubscribedEvents')
->will($this->returnValue($events));

$emitter = new EventEmitter();
Expand Down
Loading

0 comments on commit d54d2e1

Please sign in to comment.