Skip to content

Instantly share code, notes, and snippets.

@sam2332
Last active May 14, 2018 15:43
Show Gist options
  • Save sam2332/5f2fb407648e5aa69961b5a19e357758 to your computer and use it in GitHub Desktop.
Save sam2332/5f2fb407648e5aa69961b5a19e357758 to your computer and use it in GitHub Desktop.
this is a simple file based caching system
<?php
/**
* Created by Sam Rudloff.
* Date: 5/14/18
* Time: 11:26 AM
*/
class file_vault
{
public function __construct($path)
{
$this->base_path = $path;
}
private function makePath($key)
{
return $this->base_path.'/'. base64_encode($key);
}
private function encodeData($data)
{
$data = serialize($data);
return $data;
}
private function decodeData($data)
{
$data = unserialize($data);
return $data;
}
public function set($key,$data)
{
$path = $this->makePath($key);
$data = $this->encodeData($data);
file_put_contents($path, $data);
}
public function exists($key)
{
$path = $this->makePath($key);
return is_file($path);
}
public function clear($key)
{
$path = $this->makePath($key);
unlink($path);
}
public function get($key,$default=false)
{
$path = $this->makePath($key);
if (is_file($path))
{
$data = file_get_contents($path);
$data = $this->decodeData($data);
return $data;
}
else
{
return $default;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment