From 5319a9aa61fb64136dfb9367ede6caf648e3985d Mon Sep 17 00:00:00 2001 From: Oliver Stark Date: Tue, 19 Jun 2018 17:46:30 +0200 Subject: [PATCH] Initial --- .gitignore | 23 +++++++++++++++++ README.md | 16 ++++++++++++ composer.json | 34 +++++++++++++++++++++++++ src/Bootstrap.php | 63 +++++++++++++++++++++++++++++++++++++++++++++++ src/init.php | 22 +++++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Bootstrap.php create mode 100644 src/init.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0fa63fb --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +# COMPOSER +/vendor +composer.lock + + +# MISC FILES +.cache +.DS_Store +.idea +.project +.settings +*.esproj +*.sublime-workspace +*.sublime-project +*.tmproj +*.tmproject +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +config.codekit3 +prepros-6.config diff --git a/README.md b/README.md new file mode 100644 index 0000000..b2d618d --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Yii2/Craft3 memcached enabler + +This yii-extension is a memcached drop in replacement for sessions and cache. + +The file `Session` handler and the file `Cache` driver gets replaced in fortrabbit environments where Memcache is enabled - it requires the Memcache component on the Professional Stack. + + + +## Install + +Require the package: +``` +composer require fortrabbit/yii-memcached +``` + +That's it. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..e5cf77b --- /dev/null +++ b/composer.json @@ -0,0 +1,34 @@ +{ + "name": "fortrabbit/yii-memcached", + "description": "Mutes deprecation logs in production", + "version": "1.0.0", + "type": "yii2-extension", + + "keywords": ["yii2", "craftcms", "caching", "cache", "memcached", "memcache", "session"], + "license": "MIT", + "authors": [ + { + "name": "Oliver Stark", + "homepage": "https://www.fortrabbit.com" + } + ], + "support": { + "email": "os@fortrabbit.com", + "issues": "https://github.com/fortrabbit/yii-memcached/issues", + "source": "https://github.com/fortrabbit/yii-memcached", + "docs": "https://github.com/fortrabbit/yii-memcached" + }, + "require": { + "craftcms/cms": "^3.0.0" + }, + "autoload": { + "psr-4": { + "fortrabbit\\MemcachedEnabler\\": "src/" + }, + "files": ["src/init.php"] + + }, + "extra": { + "bootstrap": "fortrabbit\\MemcachedEnabler\\Bootstrap" + } +} diff --git a/src/Bootstrap.php b/src/Bootstrap.php new file mode 100644 index 0000000..3d49f79 --- /dev/null +++ b/src/Bootstrap.php @@ -0,0 +1,63 @@ + + * @since 1.0.0 + */ +class Bootstrap implements BootstrapInterface +{ + + /** + * Bootstrapper + * + * @param \yii\base\Application $app + */ + public function bootstrap($app) + { + + if (getenv('MEMCACHE_COUNT')) { + + // Set cache component + try { + $app->set('cache', [ + 'class' => MemCache::class, + 'persistentId' => getenv('MEMCACHE_PERSISTENT') ? 'cache' : null, + 'servers' => $this->getMemcachedServers(), + 'useMemcached' => true + ]); + } catch (InvalidConfigException $e) { + error_log('MemcachedEnabler: ' . $e->getMessage()); + } + } + } + + + /** + * @return array + */ + protected function getMemcachedServers(): array + { + $servers = []; + + foreach (range(1, getenv('MEMCACHE_COUNT')) as $num) { + $servers[] = [ + 'host' => getenv('MEMCACHE_HOST' . $num), + 'port' => getenv('MEMCACHE_PORT' . $num), + 'retryInterval' => 2, + 'status' => true, + 'timeout' => 2, + 'weight' => 1, + ]; + } + + return $servers; + } +} diff --git a/src/init.php b/src/init.php new file mode 100644 index 0000000..689b03c --- /dev/null +++ b/src/init.php @@ -0,0 +1,22 @@ +