Skip to content

Commit

Permalink
Improve the way you can enable caching, by providing a second paramet…
Browse files Browse the repository at this point in the history
…er to the constructor of the Parser object
  • Loading branch information
NielsLeenheer committed Apr 22, 2016
1 parent 6f13535 commit 01dfb59
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 23 deletions.
15 changes: 13 additions & 2 deletions README.md
Expand Up @@ -163,7 +163,7 @@ Enable result caching

WhichBrowser supports PSR-6 compatible cache adapters for caching results between requests. Using a cache is especially useful if you use WhichBrowser on every page of your website and a user visits multiple pages. During the first visit the headers will be parsed and the result will be cached. Upon further visits, the cached results will be used, which is much faster than having to parse the headers again and again.

There are adapters available for other types of caches, such as APC, Doctrine, Memcached, MongoDB, Redis and many more. The configuration of these adapters all differ from each other, but once configured, all you have to do is use the `setCache()` function to enable it's use by WhichBrowser. WhichBrowser has been tested to work with the adapters provided by [PHP Cache](http://php-cache.readthedocs.org/en/latest/). For a list of other packages that provide adapters see [Packagist](https://packagist.org/providers/psr/cache-implementation).
There are adapters available for other types of caches, such as APC, Doctrine, Memcached, MongoDB, Redis and many more. The configuration of these adapters all differ from each other, but once configured, all you have to do is pass it as an option when creating the `Parser` object, or use the `setCache()` function to set it afterwards. WhichBrowser has been tested to work with the adapters provided by [PHP Cache](http://php-cache.readthedocs.org/en/latest/). For a list of other packages that provide adapters see [Packagist](https://packagist.org/providers/psr/cache-implementation).

For example, if you want to enable a memcached based cache you need to install an extra composer package:

Expand All @@ -177,12 +177,23 @@ $client->addServer('localhost', 11211);

$pool = new \Cache\Adapter\Memcached\MemcachedCachePool($client);

$result = new WhichBrowser\Parser(getallheaders(), [ 'cache' => $pool ]);
```

or

```php
$client = new \Memcached();
$client->addServer('localhost', 11211);

$pool = new \Cache\Adapter\Memcached\MemcachedCachePool($client);

$result = new WhichBrowser\Parser();
$result->setCache($pool);
$result->analyse(getallheaders());
```

The `setCache()` function also supports an optional second parameter which specifies after how many seconds a cached result should be discarded. The default value is 900 seconds or 15 minutes. If you think WhichBrowser uses too much memory for caching, you should lower this value.
You can also specify after how many seconds a cached result should be discarded. The default value is 900 seconds or 15 minutes. If you think WhichBrowser uses too much memory for caching, you should lower this value. You can do this by setting the `cacheExpires` option or passing it as a second parameter to the `setCache()` function.


API reference
Expand Down
13 changes: 3 additions & 10 deletions src/Analyser.php
Expand Up @@ -15,17 +15,10 @@ class Analyser

private $headers = [];

public function __construct($options)
public function __construct($headers, $options = [])
{
if (is_string($options)) {
$this->options = (object) [ 'headers' => [ 'User-Agent' => $options ] ];
} else {
$this->options = (object) (isset($options['headers']) ? $options : [ 'headers' => $options ]);
}

if (isset($this->options->headers)) {
$this->headers = $this->options->headers;
}
$this->headers = $headers;
$this->options = (object) $options;
}

public function setData(&$data)
Expand Down
12 changes: 10 additions & 2 deletions src/Cache.php
Expand Up @@ -79,15 +79,23 @@ private function retrieveCachedData()
* @return boolean did we actually retrieve or analyse results
*/

private function analyseWithCache($headers)
private function analyseWithCache($headers, $options = [])
{
if (isset($options['cache'])) {
if (isset($options['cacheExpires'])) {
$this->setCache($options['cache'], $options['cacheExpires']);
} else {
$this->setCache($options['cache']);
}
}

if ($this->cache instanceof CacheItemPoolInterface) {
$item = $this->cache->getItem('whichbrowser-' . md5(serialize($headers)));

if ($item->isHit()) {
$this->applyCachedData($item->get());
} else {
$analyser = new Analyser($headers);
$analyser = new Analyser($headers, $options);
$analyser->setdata($this);
$analyser->analyse();

Expand Down
26 changes: 21 additions & 5 deletions src/Parser.php
Expand Up @@ -12,14 +12,15 @@ class Parser extends Main
* Create a new object that contains all the detected information
*
* @param array|string $headers Optional, an array with all of the headers or a string with just the User-Agent header
* @param array $options Optional, an array with configuration options
*/

public function __construct($headers = null)
public function __construct($headers = null, $options = [])
{
parent::__construct();

if (!is_null($headers)) {
$this->analyse($headers);
$this->analyse($headers, $options);
}
}

Expand All @@ -29,13 +30,28 @@ public function __construct($headers = null)
* @param array|string $headers An array with all of the headers or a string with just the User-Agent header
*/

public function analyse($headers)
public function analyse($headers, $options = [])
{
if ($this->analyseWithCache($headers)) {
$o = $options;

if (is_string($headers)) {
$h = [ 'User-Agent' => $headers ];
} else {
if (isset($headers['headers'])) {
$h = $headers['headers'];

unset($headers['headers']);
$o = array_merge($headers, $options);
} else {
$h = $headers;
}
}

if ($this->analyseWithCache($h, $o)) {
return;
}

$analyser = new Analyser($headers);
$analyser = new Analyser($h, $o);
$analyser->setdata($this);
$analyser->analyse();
}
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/AnalyserTest.php
Expand Up @@ -12,10 +12,10 @@ class AnalyserTest extends PHPUnit_Framework_TestCase
{
public function testCreatingAnalyserGetData()
{
$analyser = new Analyser("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)");
$analyser = new Analyser([ "User-Agent" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)" ]);

$this->assertTrue($analyser instanceof \WhichBrowser\Analyser);

$analyser->analyse();

$data = $analyser->getData();
Expand All @@ -25,10 +25,10 @@ public function testCreatingAnalyserGetData()

public function testCreatingAnalyserSetData()
{
$analyser = new Analyser("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)");
$analyser = new Analyser([ "User-Agent" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)" ]);

$this->assertTrue($analyser instanceof \WhichBrowser\Analyser);

$input = new Main();

$this->assertTrue($input instanceof \WhichBrowser\Model\Main);
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/CacheTest.php
Expand Up @@ -48,5 +48,10 @@ function countCachedItems($pool) {

$this->assertEquals($result, $parser->toArray());
$this->assertEquals(true, $parser->cached);

$parser = new Parser("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)", [ 'cache' => $pool ]);

$this->assertEquals($result, $parser->toArray());
$this->assertEquals(true, $parser->cached);
}
}

0 comments on commit 01dfb59

Please sign in to comment.