Skip to content

Commit 1cb6b8b

Browse files
author
Phu Hoang
committed
Init project
0 parents  commit 1cb6b8b

File tree

8 files changed

+157
-0
lines changed

8 files changed

+157
-0
lines changed

Model/System/Config/RedisHandler.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Used in creating options for Redis Fallback Handler config value selection
4+
*/
5+
namespace Kodbruket\RedisFallback\Model\System\Config;
6+
7+
use Magento\Framework\Option\ArrayInterface;
8+
9+
/**
10+
* Class RedisHandler
11+
*/
12+
class RedisHandler implements ArrayInterface
13+
{
14+
const FILES = 'files';
15+
const DATABASE = 'db';
16+
17+
/**
18+
* Options getter
19+
*
20+
* @return array
21+
*/
22+
public function toOptionArray()
23+
{
24+
return [
25+
[
26+
'value' => self::FILES,
27+
'label' => __('Files')
28+
],
29+
[
30+
'value' => self::DATABASE,
31+
'label' => __('Database')
32+
]
33+
];
34+
}
35+
36+
/**
37+
* Get options in "key-value" format
38+
*
39+
* @return array
40+
*/
41+
public function toArray()
42+
{
43+
return [
44+
self::FILES => __('Files'),
45+
self::DATABASE => __('Database')
46+
];
47+
}
48+
}

Session/SaveHandler.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Kodbruket\RedisFallback\Session;
4+
5+
use Magento\Framework\Session\SaveHandlerFactory;
6+
use Magento\Framework\Session\Config\ConfigInterface;
7+
use Magento\Framework\Exception\SessionException;
8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Store\Model\ScopeInterface;
10+
11+
class SaveHandler extends \Magento\Framework\Session\SaveHandler
12+
{
13+
const REDIS_FALLBACK_HANDLER_CONFIG_XML = 'system/redis_fallback/handler';
14+
15+
/**
16+
* Constructor
17+
*
18+
* @param SaveHandlerFactory $saveHandlerFactory
19+
* @param ConfigInterface $sessionConfig
20+
* @param string $default
21+
* @param ScopeConfigInterface $scopeConfig
22+
*/
23+
public function __construct(
24+
SaveHandlerFactory $saveHandlerFactory,
25+
ConfigInterface $sessionConfig,
26+
$default = self::DEFAULT_HANDLER,
27+
ScopeConfigInterface $scopeConfig
28+
) {
29+
$fallbackHandler = $scopeConfig->getValue(self::REDIS_FALLBACK_HANDLER_CONFIG_XML, ScopeInterface::SCOPE_STORE);
30+
if($fallbackHandler){
31+
$default = $fallbackHandler;
32+
}
33+
34+
parent::__construct( $saveHandlerFactory, $sessionConfig, $default);
35+
}
36+
}

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "kodbruket/module-redisfallback",
3+
"description": "Fallback Redis session to Db on failure",
4+
"type": "magento2-module",
5+
"license": [
6+
"proprietary"
7+
],
8+
"authors":[
9+
{
10+
"name":"Jimmy",
11+
"email":"phu@kodbruket.se"
12+
}
13+
],
14+
"require": {
15+
},
16+
"autoload": {
17+
"files": [
18+
"registration.php"
19+
],
20+
"psr-4": {
21+
"Kodbruket\\RedisFallback\\": ""
22+
}
23+
}
24+
}

etc/adminhtml/system.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
9+
<system>
10+
<section id="system">
11+
<group id="redis_fallback" translate="label" showInDefault="1" showInWebsite="0" showInStore="0" sortOrder="800">
12+
<label>Redis Fallback</label>
13+
<field id="handler" translate="label" type="select" sortOrder="0" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
14+
<label>Fallback Handler</label>
15+
<source_model>Kodbruket\RedisFallback\Model\System\Config\RedisHandler</source_model>
16+
</field>
17+
</group>
18+
</section>
19+
</system>
20+
</config>

etc/config.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Store/etc/config.xsd">
3+
<default>
4+
<system>
5+
<redis_fallback>
6+
<handler>db</handler>
7+
</redis_fallback>
8+
</system>
9+
</default>
10+
</config>

etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
2+
<preference for="Magento\Framework\Session\SaveHandler" type="Kodbruket\RedisFallback\Session\SaveHandler" />
3+
</config>

etc/module.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3+
<module name="Kodbruket_RedisFallback" setup_version="1.0.0">
4+
<sequence>
5+
</sequence>
6+
</module>
7+
</config>

registration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use \Magento\Framework\Component\ComponentRegistrar;
4+
5+
ComponentRegistrar::register(
6+
ComponentRegistrar::MODULE,
7+
'Kodbruket_RedisFallback',
8+
__DIR__
9+
);

0 commit comments

Comments
 (0)