Skip to content

Commit

Permalink
add(userStorage): Support for Redis User Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Saksham Gupta authored and rohitesh-wingify committed Jan 12, 2024
1 parent ec1dd0d commit 539c9ee
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.68.0] - 2024-01-12

### Added

- Support for Redis user storage

## [1.66.0] - 2023-11-21

### Fixed
Expand Down
1 change: 1 addition & 0 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
require_once 'src/Logger/DefaultLogger.php';
require_once 'src/Logger/Logger.php';
require_once 'src/Utils/Constants.php';
require_once 'src/Storage/RedisUserStorage.php';
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"monolog/monolog": "^1.0 || ^2.0",
"ramsey/uuid": "^3.8 || ^4.0",
"justinrainbow/json-schema": "^5.2",
"vwo/vwo-sdk-log-messages": ">=0.10.0"
"vwo/vwo-sdk-log-messages": ">=0.10.0",
"predis/predis": "^2.2"
},
"require-dev": {
"phpunit/phpunit": "^4.0",
Expand Down
115 changes: 115 additions & 0 deletions src/Storage/RedisUserStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/**
* Copyright 2019-2022 Wingify Software Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace vwo\Storage;

use Exception as Exception;
use Predis\Client;
use Monolog\Logger as Logger;
use vwo\Services\LoggerService as LoggerService;

/**
* Interface UserStorageInterface
*
* user interface should be included and used to set and get user-campaign mapping
*/
class RedisUserStorage implements UserStorageInterface
{
const CLASSNAME = 'vwo/Storage/RedisUserStorage';
private $redisConnection;
private $redisDatabase;


/**
* Constructor for initializing the RedisUserStorageService.
*
* @param RedisConfig $redisConfig Configuration object containing Redis server details.
* @throws Exception If there is an issue connecting to Redis.
*/
public function __construct($redisConfig)
{
try {
$options = parse_url($redisConfig['url']);

// Initialize Redis connection here
// Connect to the Redis server using the host and port from the URL
$this->redisConnection = new Client(['host' => $options['host'], 'port' => $options['port'], 'password'=> $redisConfig['password']]);

// Assign the Redis database connection
$this->redisDatabase = $this->redisConnection;
} catch (Exception $ex) {
// Handle the exception, e.g., log it or take appropriate action
LoggerService::log(Logger::ERROR, 'Error connecting to Redis ', [], self::CLASSNAME);
throw $ex;
}
}


/**
* Get user-campaign mapping for the given user ID and campaign key.
*
* @param string $userId User ID.
* @param string $campaignKey Campaign key.
* @return array|null The user-campaign mapping as an associative array, or null if not found.
*/
public function Get($userId, $campaignKey)
{
if ($this->redisDatabase !== null) {
try {
$key = $campaignKey . ':' . $userId;
$result = $this->redisDatabase->get($key);

if (!empty($result)) {
return json_decode($result, true); // Pass true to decode as an associative array
}
} catch (Exception $ex) {
LoggerService::log(Logger::ERROR, 'Error while getting data from Redis ', ['{userId}' => $userId], self::CLASSNAME);
}
} else {
LoggerService::log(Logger::ERROR, 'Redis client not initialized ', ['{userId}' => $userId], self::CLASSNAME);
}

return null;
}
/**
* @param $campaignInfo
* @return array
*/

/**
* Set user-campaign mapping using the provided user storage map.
*
* @param $campaignInfo User storage map to be stored.
*/
public function Set($campaignInfo)
{
if ($this->redisDatabase !== null) {
$key = $campaignInfo['campaignKey'] . ':' . $campaignInfo['userId'];
$data = json_encode($campaignInfo);

try {
$this->redisDatabase->set($key, $data);
} catch (Exception $ex) {
LoggerService::log(Logger::ERROR, 'Error while setting data in Redis ', ['{userId}' => $campaignInfo['userId']], self::CLASSNAME);
throw $ex;
}
} else {
LoggerService::log(Logger::ERROR, 'Redis client not initialized ', ['{userId}' => $campaignInfo['userId']], self::CLASSNAME);
}
}
}
2 changes: 1 addition & 1 deletion src/Utils/ImpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ImpressionBuilder
/**
* sdk version for api hit
*/
const SDK_VERSION = '1.66.0';
const SDK_VERSION = '1.68.0';
/**
* sdk langauge for api hit
*/
Expand Down
9 changes: 8 additions & 1 deletion src/VWO.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use vwo\Logger\VWOLogger as VWOLogger;
use vwo\Core\Bucketer as Bucketer;
use vwo\Core\VariationDecider as VariationDecider;
use vwo\Storage\RedisUserStorage;
use vwo\Utils\LogMessagesUtil;

/***
Expand Down Expand Up @@ -138,7 +139,13 @@ function __construct($config)
if (isset($config['userStorageService']) && ($config['userStorageService'] instanceof UserStorageInterface)) {
$this->_userStorageObj = $config['userStorageService'];
$usageStats['ss'] = 1;
} else {
}else if (isset($config['redisConfig'])) {
$this->_userStorageObj = new RedisUserStorage($config['redisConfig']);
//var_dump($this->_userStorageObj);
$config['userStorageService'] = $this->_userStorageObj;
$usageStats['ss'] = 1;
}
else {
$this->_userStorageObj = '';
}

Expand Down

0 comments on commit 539c9ee

Please sign in to comment.