Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
simple file base cache for php

How to Use

Set

require 'fvs.php';
$cache = new Fvs();

$cache->set('hoge', 'hello'); // hello

Get

require 'fvs.php';

$cache = new Fvs();
$cache->set('hoge', 'hello'); // hello
$cache->get('hoge'); // hello

List Keys

require 'fvs.php';
$cache = new Fvs();

foreach(range(1, 10) as $i) {
    $cache->set('nick'. $i, $i);
}

$cache->keys();

// array(10) {
//   [0]=>
//   string(6) " nick1"
//   [1]=>
//   string(7) " nick10"
//   [2]=>
//   string(6) " nick2"
//   [3]=>
//   string(6) " nick3"
//   [4]=>
//   string(6) " nick4"
//   [5]=>
//   string(6) " nick5"
//   [6]=>
//   string(6) " nick6"
//   [7]=>
//   string(6) " nick7"
//   [8]=>
//   string(6) " nick8"
//   [9]=>
//   string(6) " nick9"
// }

Delete

require 'fvs.php';
$cache = new Fvs();

$cache->set('hoge', 'hello');
$cache->del('hoge'); // hello
$cache->get('hoge'); // null

Multi Key Delete

require 'fvs.php';
$cache = new Fvs();

$cache->set('hoge', 1);
$cache->set('fuga', 1);
$cache->del(['hoge','fuga']) // 2
$cache->get('hoge'); // null
$cache->set('hoge'); // null

Flush All

require 'fvs.php';
$cache = new Fvs();

foreach(range(1, 10) as $i) {
    $fvs->set('nick'. $i, $i);
}

foreach (range(1, 10) as $i) {
    echo ($fvs->get('nick'. $i). PHP_EOL);
}

$fvs->flushall();
<?php
class Fvs
{
private $cache_path;
private $file_ext;
/**
* @param string $cache_path
* @param string $file_ext
*/
public function __construct($cache_path = '.caches/', $file_ext = '.cache')
{
if (!file_exists($cache_path)) {
mkdir($cache_path);
}
$this->cache_path = $cache_path;
$this->file_ext = $file_ext;
}
/**
* @param $key_name
* @return mixed
*/
public function get($key_name)
{
$file_name = $this->file_name($key_name);
if (file_exists($file_name)) {
return file_get_contents($file_name);
}
}
/**
* @param string $key_name
* @param $value
* @return mixed
*/
public function set($key_name, $value)
{
file_put_contents($this->file_name($key_name), $value);
return $value;
}
/**
* @param mixed $key_names
* @return int
*/
public function del($key_names)
{
if (is_array($key_names)) {
return $this->multi_del($key_names);
}
return $this->single_del($key_names);
}
public function flushall()
{
if ($dirHandle = opendir($this->cache_path)) {
while (false !== ($fileName = readdir($dirHandle))) {
if ($fileName !== ".") {
unlink($this->cache_path . $fileName);
}
}
closedir($dirHandle);
}
}
/**
* @return array
*/
public function keys()
{
$search_value = $this->cache_path . '*.fvs';
$keys = [];
$values = glob($search_value);
foreach ($values as $v) {
$k_name = preg_replace("/(.+)(\.[^.]+$)/", "$1", str_replace($this->cache_path, ' ', $v));
$keys[] = $k_name;
}
return $keys;
}
/**
* @param array $key_names
* @return int
*/
private function multi_del($key_names)
{
$result = 0;
foreach ($key_names as $k) {
$file_name = $this->file_name($k);
if (unlink($file_name)) {
$result += 1;
}
}
return $result;
}
/**
* @param string $key_name
* @return int
*/
private function single_del($key_name)
{
if (unlink($this->file_name($key_name))) {
return 1;
}
return 0;
}
/**
* @param string $key_name
* @return string
*/
private function file_name($key_name)
{
return $this->cache_path . $key_name . $this->file_ext;
}
}
<?php
require 'fvs.php';
$fvs = new Fvs();
foreach(range(1, 10) as $i) {
$fvs->set('nick'. $i, $i);
}
foreach (range(1, 10) as $i) {
echo ($fvs->get('nick'. $i). PHP_EOL);
}
$fvs->flushall();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment