<commit_before><?php
/**
 * Key based limiter
 */
namespace Minds\Core\Security\RateLimits;

use Minds\Traits\MagicAttributes;
use Minds\Core\Di\Di;
use Minds\Core\Data\Redis\Client as RedisServer;
use Minds\Core\Logger;

/**
 * @method KeyValueLimiter setKey(string $key)
 * @method KeyValueLimiter setValue(string $value)
 * @method KeyValueLimiter setMax(int $max)
 * @method KeyValueLimiter setSeconds(int $seconds)
 */
class KeyValueLimiter
{
    use MagicAttributes;

    /** @var RedisServer */
    private $redis;

    /** @var Logger\Log */
    private $logger;

    /** @var string */
    private $key;

    /** @var string */
    private $value;

    /** @var int */
    private $max = 300;

    /** @var int */
    private $seconds = 300; // 5 minutes

    /**
     * @param RedisServer $redis
     * @param Condfig $config
     */
    public function __construct($redis = null, $config = null, $logger = null)
    {
        $config = $config ?? Di::_()->get('Config');
        $this->redis = $redis ?: new RedisServer();
        $this->redis->connect($config->redis['master']);
        $this->logger = $logger ?? Di::_()->get('Logger');
    }

    /**
     * Checks and increment the rate limit
     * @return bool
     */
    public function checkAndIncrement(): bool
    {
        $recordKey = "ratelimit:$this->key-$this->value:$this->seconds";
        $count = (int) $this->redis->get("$recordKey");
        
        if ($count >= $this->max) {
            $this->logger->warn("[RateLimit]: $recordKey was hit with $this->max");
            throw new RateLimitExceededException();
        }

        $this->redis->multi()
            ->incr($recordKey)
            ->expire($recordKey, $this->seconds)
            ->exec();

        return true;
    }
}<commit_msg>(fix): do not connect to redis on constructor, only when we are required to<commit_after><?php
/**
 * Key based limiter
 */
namespace Minds\Core\Security\RateLimits;

use Minds\Traits\MagicAttributes;
use Minds\Core\Di\Di;
use Minds\Core\Data\Redis\Client as RedisServer;
use Minds\Core\Logger;

/**
 * @method KeyValueLimiter setKey(string $key)
 * @method KeyValueLimiter setValue(string $value)
 * @method KeyValueLimiter setMax(int $max)
 * @method KeyValueLimiter setSeconds(int $seconds)
 */
class KeyValueLimiter
{
    use MagicAttributes;

    /** @var Config */
    private $config;

    /** @var RedisServer */
    private $redis;

    /** @var bool */
    private $redisIsConnected = false;

    /** @var Logger\Log */
    private $logger;

    /** @var string */
    private $key;

    /** @var string */
    private $value;

    /** @var int */
    private $max = 300;

    /** @var int */
    private $seconds = 300; // 5 minutes

    /**
     * @param RedisServer $redis
     * @param Condfig $config
     */
    public function __construct($redis = null, $config = null, $logger = null)
    {
        $this->config = $config ?? Di::_()->get('Config');
        $this->redis = $redis ?: new RedisServer();
        $this->logger = $logger ?? Di::_()->get('Logger');
    }

    /**
     * Checks and increment the rate limit
     * @return bool
     */
    public function checkAndIncrement(): bool
    {
        $recordKey = "ratelimit:$this->key-$this->value:$this->seconds";
        $count = (int) $this->getRedis()->get("$recordKey");
        
        if ($count >= $this->max) {
            $this->logger->warn("[RateLimit]: $recordKey was hit with $this->max");
            throw new RateLimitExceededException();
        }

        $this->getRedis()->multi()
            ->incr($recordKey)
            ->expire($recordKey, $this->seconds)
            ->exec();

        return true;
    }

    /**
     * Get our redis connection
     * @return RedisServer
     */
    private function getRedis(): RedisServer
    {
        if (!$this->redisIsConnected) {
            $this->redis->connect($this->config->redis['master']);
            $this->redisIsConnected = true;
        }
        return $this->redis;
    }
}
{'fraction_non_alphanumeric': 0.16168410563029398, 'fraction_numerical': 0.003487792725460887, 'mean_word_length': 2.3739495798319328, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5516247', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1235, 'n_words': 288}