commit 30eb772a66ddd9c94a9e93fe0a11bf61549c7c77
Author: finwo <finwo@pm.me>
Date: Thu, 14 Apr 2016 01:10:12 +0200
Project init
Diffstat:
| A | .gitignore | | | 2 | ++ |
| A | composer.json | | | 25 | +++++++++++++++++++++++++ |
| A | composer.lock | | | 114 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/Memcached.php | | | 92 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 233 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,2 @@
+/vendor/
+/.idea/
diff --git a/composer.json b/composer.json
@@ -0,0 +1,25 @@
+{
+ "name": "finwo/cache-memcached",
+ "minimum-stability": "dev",
+ "authors": [
+ {
+ "name": "Robin Bron",
+ "email": "robin@finwo.nl"
+ }
+ ],
+ "autoload": {
+ "psr-4": { "Finwo\\Cache\\": "src" }
+ },
+ "repositories": [{
+ "type": "vcs",
+ "url": "git@localhost:finwo/php-cache.git"
+ },{
+ "type": "vcs",
+ "url": "git@github.com:finwo/data-tools-php.git"
+ }],
+ "require": {
+ "php": ">=5.3",
+ "ext-memcached": "*",
+ "finwo/cache": "*@dev"
+ }
+}
diff --git a/composer.lock b/composer.lock
@@ -0,0 +1,114 @@
+{
+ "_readme": [
+ "This file locks the dependencies of your project to a known state",
+ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
+ "This file is @generated automatically"
+ ],
+ "hash": "27e32eb8461351e6b9a90406602cfb5e",
+ "content-hash": "c358e78e6cbe18d44e36833437d9f062",
+ "packages": [
+ {
+ "name": "finwo/cache",
+ "version": "dev-master",
+ "source": {
+ "type": "git",
+ "url": "git@localhost:finwo/php-cache.git",
+ "reference": "ed9a42f4e1fc4f72cdef6a0664a379152727db0f"
+ },
+ "require": {
+ "finwo/data-tools": "*@dev",
+ "php": ">=5.3"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-4": {
+ "Finwo\\Cache\\": "src"
+ }
+ },
+ "authors": [
+ {
+ "name": "Robin Bron",
+ "email": "robin@finwo.nl"
+ }
+ ],
+ "time": "2016-04-13 22:36:04"
+ },
+ {
+ "name": "finwo/data-tools",
+ "version": "dev-master",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/finwo/data-tools-php.git",
+ "reference": "25ef99b08544efd3056213573a3796b5a4824080"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/finwo/data-tools-php/zipball/25ef99b08544efd3056213573a3796b5a4824080",
+ "reference": "25ef99b08544efd3056213573a3796b5a4824080",
+ "shasum": ""
+ },
+ "require": {
+ "finwo/property-accessor": "dev-master"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-4": {
+ "Finwo\\Datatools\\": "src/"
+ }
+ },
+ "authors": [
+ {
+ "name": "Robin Bron",
+ "email": "robin@finwo.nl"
+ }
+ ],
+ "support": {
+ "source": "https://github.com/finwo/data-tools-php/tree/master",
+ "issues": "https://github.com/finwo/data-tools-php/issues"
+ },
+ "time": "2016-04-13 17:36:03"
+ },
+ {
+ "name": "finwo/property-accessor",
+ "version": "dev-master",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/finwo/property-accessor.git",
+ "reference": "43a9b49f82ba566800068427c12d28ac48efa8e3"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/finwo/property-accessor/zipball/43a9b49f82ba566800068427c12d28ac48efa8e3",
+ "reference": "43a9b49f82ba566800068427c12d28ac48efa8e3",
+ "shasum": ""
+ },
+ "type": "library",
+ "autoload": {
+ "psr-4": {
+ "Finwo\\PropertyAccessor\\": "src/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "authors": [
+ {
+ "name": "Robin Bron",
+ "email": "robin@finwo.nl"
+ }
+ ],
+ "time": "2016-04-13 09:05:33"
+ }
+ ],
+ "packages-dev": [],
+ "aliases": [],
+ "minimum-stability": "dev",
+ "stability-flags": {
+ "finwo/cache": 20
+ },
+ "prefer-stable": false,
+ "prefer-lowest": false,
+ "platform": {
+ "php": ">=5.3",
+ "ext-memcached": "*"
+ },
+ "platform-dev": []
+}
diff --git a/src/Memcached.php b/src/Memcached.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace Fiwno\Cache;
+
+use Finwo\Cache\Cache;
+
+/**
+ * Implements memcached
+ * Handles refreshing in a server-friendly way
+ */
+class Memcached extends Cache
+{
+ /**
+ * @var \Memcached
+ */
+ protected $memcached;
+
+ /**
+ * @var string
+ */
+ protected $server = '127.0.0.1';
+
+ /**
+ * @var int
+ */
+ protected $port = 11211;
+
+ protected function getMemcached()
+ {
+ if (null === $this->memcached) {
+ $this->memcached = new \Memcached();
+ }
+ }
+
+ public function __construct($options = array())
+ {
+ // Override default options
+ foreach($options as $key => $value) {
+ if (isset($this->{$key})) {
+ $this->{$key} = $value;
+ }
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function fetch($key = '', $ttl = 30)
+ {
+ // Generate seperate keys for fetching
+ $key_lock = sprintf("%s_lock", $key);
+ $key_data = sprintf("%s_data", $key);
+ $key_ttl = sprintf("%s_ttl", $key);
+
+ // Fetch TTL
+ $data_data = $this->memcached->get($key_data);
+
+ // Return data if the current TTL is still valid
+ if ($this->memcached->get($key_ttl)) {
+ return $data_data;
+ }
+
+ // Return data if we're already "refreshing"
+ if ($this->memcached->get($key_lock)) {
+ return $data_data;
+ }
+
+ // Mark we're refreshing for ttl/10
+ // Also give it 2 seconds, because some operations take time
+ $this->memcached->add($key_lock, true, max(2,$ttl/10));
+
+ // And return that we don't have recent data
+ return false;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function store($key = '', $value, $ttl = 30)
+ {
+ // Generate seperate keys for storing
+ $key_data = sprintf("%s_data", $key);
+ $key_ttl = sprintf("%s_ttl", $key);
+
+ // Store data
+ $this->memcached->add($key_data, $value, max(3600, $ttl*10));
+
+ // Store TTL
+ $this->memcached->add($key_ttl, true, $ttl);
+ }
+}
+\ No newline at end of file