Skip to content

Commit

Permalink
Add memchached implementation for token cache
Browse files Browse the repository at this point in the history
  • Loading branch information
regdos committed Apr 7, 2016
1 parent 28a55eb commit e4a0927
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/OpenPayU/Oauth/Cache/OauthCacheMemcached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

class OauthCacheMemcached implements OauthCacheInterface
{
private $memcached;

/**
* @param string $host
* @param int $port
* @param int $weight
* @throws OpenPayU_Exception_Configuration
*/
public function __construct($host = 'localhost', $port = 11211, $weight = 0)
{
if (!class_exists('Memcached')) {
throw new OpenPayU_Exception_Configuration('PHP Memcached extension not installed.');
}

$this->memcached = new Memcached('PayU');
$this->memcached->addServer($host, $port, $weight);
$stats = $this->memcached->getStats();
if ($stats[$host . ':' . $port]['pid'] == -1) {
throw new OpenPayU_Exception_Configuration('Problem with connection to memcached server [host=' . $host . '] [port=' . $port . '] [weight=' . $weight . ']');
}
}

public function get($key)
{
$cache = $this->memcached->get($key);
return $cache === false ? null : unserialize($cache);
}

public function set($key, $value)
{
return $this->memcached->set($key, serialize($value));
}

public function invalidate($key)
{
return $this->memcached->delete($key);
}

}
1 change: 1 addition & 0 deletions lib/openpayu.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
require_once('OpenPayU/Oauth/OauthResultClientCredentials.php');
require_once('OpenPayU/Oauth/Cache/OauthCacheInterface.php');
require_once('OpenPayU/Oauth/Cache/OauthCacheFile.php');
require_once('OpenPayU/Oauth/Cache/OauthCacheMemcached.php');

require_once('OpenPayU/ResultError.php');

Expand Down

0 comments on commit e4a0927

Please sign in to comment.