Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching #33

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@ $data = array(
)
);

$filters = new \Expose\FilterCollection();
//cache
$cache = new Expose\Cache\File();
$cache->setPath( "cache-folder-path");

// with cache
$filters = new \Expose\FilterCollection($cache);
// without cache
// $filters = new \Expose\FilterCollection();
$filters->load();

//instantiate a PSR-3 compatible logger
$logger = new \Expose\Log\Mongo();

$manager = new \Expose\Manager($filters, $logger);
// with cache
$manager->setCache($cache);
$manager->run($data);

echo 'impact: '.$manager->getImpact()."\n"; // should return 8
Expand Down
5 changes: 3 additions & 2 deletions src/Expose/Cache/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public function get($key)
$cacheFile = $this->getPath().'/'.$hash.'.cache';

if (!is_file($cacheFile)) {
return null;
return false;
}
return unserialize(file_get_contents($cacheFile));
$t = file_get_contents($cacheFile);
return (false !== $t) ? unserialize($t) : false;
}

/**
Expand Down
44 changes: 40 additions & 4 deletions src/Expose/FilterCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ class FilterCollection implements \ArrayAccess, \Iterator, \Countable
private $filterPath = 'filter_rules.json';
private $filterData = array();
private $index = 0;
private $cache;

public function __construct( \Expose\Cache $cache = null) {
if (!is_null($cache))
$this->setCache($cache);
}

public function rewind()
{
$this->index = 0;
Expand Down Expand Up @@ -62,12 +68,22 @@ public function offsetUnset($offset)

public function load($path = null)
{
$loadFile = __DIR__.'/'.$this->filterPath;
if ($path !== null && is_file($path)) {
$loadFile = $path;
$data = false;
$cache = $this->getCache();
if ( !is_null($cache)) {
$data = $cache->get('filters');
}

$data = json_decode(file_get_contents($loadFile));
if (false === $data) {
$loadFile = __DIR__.'/'.$this->filterPath;
if ($path !== null && is_file($path)) {
$loadFile = $path;
}
$data = json_decode(file_get_contents($loadFile));
if ( !is_null($cache))
$cache->save('filters', $data);
}

$this->setFilterData($data->filters);
}

Expand Down Expand Up @@ -124,4 +140,24 @@ public function addFilter(\Expose\Filter $filter)
{
$this->filterData[] = $filter;
}

/**
* Set the cache object
*
* @param ExposeCache $cache Cache instance
*/
public function setCache(\Expose\Cache $cache)
{
$this->cache = $cache;
}

/**
* Get the current cache instance
*
* @return mixed Either a \Expose\Cache instance or null
*/
public function getCache()
{
return $this->cache;
}
}
71 changes: 57 additions & 14 deletions src/Expose/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class Manager
*/
private $cache = null;



/**
* Init the object and assign the filters
*
Expand All @@ -94,7 +96,6 @@ public function __construct(
)
{
$this->setFilters($filters);

if ($logger !== null) {
$this->setLogger($logger);
}
Expand All @@ -111,7 +112,7 @@ public function __construct(
public function run(array $data, $queueRequests = false, $notify = false)
{
$this->getLogger()->info('Executing on data '.md5(print_r($data, true)));

if ($queueRequests === true) {
$this->logRequest($data);
return true;
Expand All @@ -126,15 +127,19 @@ public function run(array $data, $queueRequests = false, $notify = false)

// Check our threshold to see if we even need to send
$threshold = $this->getThreshold();


if ($threshold !== null && $impact >= $threshold && $notify === true) {
return $this->sendNotification($filterMatches);
} else if ($threshold === null && $notify === true) {
return $this->sendNotification($filterMatches);
}

return true;
}



/**
* Send the notification of the matching filters
*
Expand All @@ -161,18 +166,23 @@ public function runFilters($data, $path, $lvl = 0)
{
$filterMatches = array();
$restrictions = $this->getRestrictions();
$sig = md5(print_r($data, true));
if (is_array($data))
$data = new \ArrayIterator($data);
$data->rewind();

$sig = md5(print_r($data, true));
$cache = $this->getCache();

if ($cache !== null) {
$cacheData = $cache->get($sig);
if ($cacheData !== null) {
return $cacheData;
if (false !== $cacheData) {
$this->reports = $cacheData['reports'];
$this->impact = $cacheData['impact'];
$this->getLogger()->info('Data retrieved from cache - '.$sig);
return $cacheData['filterMatches'];
}
}

$data = new \ArrayIterator($data);
$data->rewind();
while($data->valid() && !$this->impactLimitReached()) {
$index = $data->key();
$value = $data->current();
Expand All @@ -181,7 +191,6 @@ public function runFilters($data, $path, $lvl = 0)
if (count($path) > $lvl) {
$path = array_slice($path, 0, $lvl);
}

$path[] = $index;

// see if it's an exception
Expand All @@ -192,13 +201,12 @@ public function runFilters($data, $path, $lvl = 0)

if (is_array($value)) {
$l = $lvl+1;
$filterMatches = array_merge(
$filterMatches = @array_merge(
$filterMatches,
$this->runFilters($value, $path, $l)
);
continue;
}

$p = implode('.', $path);

// See if we have restrictions & if the path matches
Expand All @@ -210,11 +218,15 @@ public function runFilters($data, $path, $lvl = 0)
continue;
}

$this->processFilters($value, $index, $path);
$filterMatches = array_merge($filterMatches, $this->processFilters($value, $index, $path));
}

if ($cache !== null) {
$cache->save($sig, $filterMatches);
$cache->save($sig, array (
'reports' => $this->reports,
'impact' => $this->impact,
'filterMatches' => $filterMatches
));
}
return $filterMatches;
}
Expand All @@ -227,11 +239,14 @@ public function runFilters($data, $path, $lvl = 0)
*/
protected function processFilters($value, $index, $path)
{

$filtersMatched = array();
$filters = $this->getFilters();
$filters->rewind();
while($filters->valid() && !$this->impactLimitReached()) {
$filter = $filters->current();
$filters->next();

if ($filter->execute($value) === true) {
$this->getLogger()->info(
'Match found on Filter ID '.$filter->getId(),
Expand All @@ -241,10 +256,11 @@ protected function processFilters($value, $index, $path)
$report = new \Expose\Report($index, $value, $path);
$report->addFilterMatch($filter);
$this->reports[] = $report;

$this->impact += $filter->getImpact();
$filtersMatched[] = $filter;
}
}
return $filtersMatched;
}

/**
Expand Down Expand Up @@ -632,4 +648,31 @@ public function export($format = 'text')
}
return null;
}

/**
* Get Filter's detection status from given Id
*
* @param int $id Filter's ID
* @return boolean
*/
public function isFilterIdDetected($id)
{
if ( empty($id) || (int)$id == 0)
return false;
$id = (int) $id;
if ( !empty($this->reports) ) {
foreach ( $this->reports as $report)
{
foreach ($report->getFilterMatch() as $filter) {
if ( (int) $filter->getId() == $id )
{
return true;
}
}
}
}
return false;
}


}