Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Stark committed Jun 19, 2018
0 parents commit 5319a9a
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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"
}
}
63 changes: 63 additions & 0 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace fortrabbit\MemcachedEnabler;

use yii\base\BootstrapInterface;
use yii\base\InvalidConfigException;
use yii\caching\MemCache;

/**
* Class Bootstrap
*
* @author Oliver Stark <[email protected]>
* @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;
}
}
22 changes: 22 additions & 0 deletions src/init.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
// Very early session handler init
// This file is loaded via composer autoload.files

if (getenv('MEMCACHE_COUNT')) {

$handlers = [];
foreach (range(1, getenv('MEMCACHE_COUNT')) as $num) {
$handlers[] = getenv('MEMCACHE_HOST' . $num) . ':' . getenv('MEMCACHE_PORT' . $num);
}

// session config
ini_set('session.save_handler', 'memcached');
ini_set('session.save_path', implode(',', $handlers));

if (getenv('MEMCACHE_COUNT') == 2) {
ini_set('memcached.sess_number_of_replicas', 1);
ini_set('memcached.sess_consistent_hash', 1);
ini_set('memcached.sess_binary', 1);
}
}

0 comments on commit 5319a9a

Please sign in to comment.