Skip to content

Commit

Permalink
Added memcache::increment()
Browse files Browse the repository at this point in the history
  • Loading branch information
Iunusov committed Nov 1, 2020
1 parent 94c0a8d commit 9fe8d5b
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions api/memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,33 @@

$mc = false;

function connect()
function memcache()
{
global $mc;
if ($mc) return $mc;
$mc = new \Memcache;
$mc->connect(MEMCACHE_IP, MEMCACHE_PORT);
return $mc;
}

function get($key)
{
global $mc;
if (!$mc)
{
connect();
}
$key = md5(strtoupper($key));
return $mc->get($key);
return memcache()->get(md5(strtoupper($key)));
}

function set($key, $value)
{
global $mc;
if (!$mc)
{
connect();
}
$flags = 0;
$key = md5(strtoupper($key));
if ($mc->replace($key, $value, $flags, MEMCACHE_EXPIRY_SECONDS) == false)
if (memcache()->replace($key, $value, $flags, MEMCACHE_EXPIRY_SECONDS) == false)
{
return $mc->set($key, $value, $flags, MEMCACHE_EXPIRY_SECONDS);
return memcache()->set($key, $value, $flags, MEMCACHE_EXPIRY_SECONDS);
}
return true;
}

function increment($key, $value = 1)
{
return memcache()->increment($key, $value);
}
?>

0 comments on commit 9fe8d5b

Please sign in to comment.