memele
6 months ago

How to create a php file caching mechanism?

Posted 6 months ago by memele

I applied for a Junior PHP Developer position and got a test that I need to finish today. The idea is to get data from their API and cache it for a set amount of time. I got the data, that part was easy, but I'm having trouble writing the set method for caching (I need to use an interface they supplied).

I know they want to test my knowledge of OOP and I thought I have the basics down but I guess this is too advanced for me. I have rewatched PHP OOP videos (inheritance, messages 101..) at least five times and I still don't fully get it.

Am I aiming too high and not ready for a job?

Any help would be greatly appreciated. Here's the code I have so far. Just to make it clear, I don't want an exact answer but maybe some pointers in the right direction? Thanks.

$result = $response->getBody(); //Data that needs to be cached
$jsonToArray = json_decode($result, true); //Needs to be stored as an array

interface CacheInterface 
{
    public function set(string $key, $value, int $duration);
    public function get(string $key);
}



class CacheController 
{
    private $cache;

    public function __construct(CacheInterface $cache) {
        $this->cache = $cache;
    }
}

class Cache implements CacheInterface 
{
    
    public function set(string $key, $value, int $duration = 60*5) {
        //key->filename?
        //value->json
    }


    public function get(string $key) {
        //if file exists, return file
        //else return null
    }

}

Please sign in or create an account to participate in this conversation.