-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oliver Stark
committed
Jun 19, 2018
0 parents
commit 5319a9a
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|