Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
vendor
.phpunit.result.cache
config.php
77 changes: 0 additions & 77 deletions CacheManager.php

This file was deleted.

12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM php:8.1

RUN apt-get update && apt-get install -y libmemcached-dev zlib1g-dev
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install memcached extension
RUN pecl install memcached
RUN docker-php-ext-enable memcached

# Install redis extension
RUN pecl install redis
RUN docker-php-ext-enable redis.so
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
run: composer_install run_cache_manager

test: composer_install
docker compose run --rm cache_manager ./vendor/phpunit/phpunit/phpunit --verbose ./tests/TestCache.php
composer_install:
docker compose run --rm cache_manager composer install
run_cache_manager: composer_install
docker compose run --rm cache_manager php index.php
config:
cp config.php.example config.php
prun:
docker system prune -a
docker volume prune
down:
docker compose down
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# fullstack_code_challenges

Identify the code smells in the CacheManager Implementation and rewrite CacheManager following all the best practices you know.
This implementation was developed with SOLID and design patterns (singleton, factory, adapter).

## Requirements

---

* docker
* docker compose
* make

## Run

---

It will need config file for connect to cache server.

First create config file: `make config`

For **run** index.php file: `make run`

For **run tests** use: `make test`

Please push your code in a different branch.
33 changes: 33 additions & 0 deletions Src/CacheFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App;

use Exception;

class CacheFactory
{
private static array $instances = [];

/**
* @throws Exception
*/
public static function make(string $type): cacheAdapterInterface
{

$conf = (include(__DIR__.'/../config.php'))['cache'];

return match ($type) {
MemcachedAdapter::class => self::getInstance(MemcachedAdapter::class, $conf['memcached']['host'], $conf['memcached']['port']),
RedisAdapter::class => self::getInstance(RedisAdapter::class, $conf['redis']['host'], $conf['redis']['port']),
default => throw new Exception("Cache Manager Not Found"),
};
}

private static function getInstance(string $cacheAdapterClassName, string $host, string $port): cacheAdapterInterface
{
if (!isset(self::$instances[$cacheAdapterClassName])) {
self::$instances[$cacheAdapterClassName] = new $cacheAdapterClassName($host, $port);
}
return self::$instances[$cacheAdapterClassName];
}
}
34 changes: 34 additions & 0 deletions Src/MemcachedAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App;

use Exception;

class MemcachedAdapter implements cacheAdapterInterface
{
protected \Memcached $cache;

public function __construct($host,$port)
{
$this->cache = new \Memcached();
$this->cache->addServer($host, $port);
}

public function set(string $key, string $value, string $is_compressed = null, string $ttl = null)
{
$this->cache->set($key, $value, $is_compressed);
}

public function get(string $key): mixed
{
return $this->cache->get($key);
}

/**
* @throws Exception
*/
public function lpush(string $key, string $value)
{
throw new Exception("method not supported");
}
}
29 changes: 29 additions & 0 deletions Src/RedisAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App;

class RedisAdapter implements cacheAdapterInterface
{
protected \Redis $cache;

public function __construct($host,$port)
{
$this->cache = new \Redis();
$this->cache->connect($host, $port);
}

public function set(string $key, string $value, string $is_compressed = null, string $ttl = null)
{
$this->cache->set($key, $value, $ttl);
}

public function get(string $key)
{
return $this->cache->get($key);
}

public function lpush(string $key, string $value)
{
$this->cache->lPush($key, $value);
}
}
12 changes: 12 additions & 0 deletions Src/cacheAdapterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App;

interface cacheAdapterInterface
{
public function set(string $key, string $value, string $is_compressed = null, string $ttl = null);

public function get(string $key);

public function lpush(string $key, string $value);
}
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"require": {
"ext-memcached": "*",
"ext-redis": "*"
},
"autoload": {
"psr-4": {
"App\\" : "Src/"
}
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"autoload-dev": {
"classmap": [
"tests/"
]
}
}
Loading