Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
erika committed May 21, 2020
0 parents commit 844be3c
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
vendor/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 minicli

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# minicache

A simple JSON file cache built for minicli (but can be used in any app)
17 changes: 17 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "minicli/minicache",
"type": "library",
"description": "A simple json file cache for minicli",
"license": "MIT",
"homepage": "https://github.com/minicli/minicli",
"keywords": ["micro","cache","json"],
"autoload": {
"psr-4": {
"Minicli\\Minicache\\": "src/"
}
},
"require": {
"minicli/minicli": "^1.0",
"ext-json": "*"
}
}
71 changes: 71 additions & 0 deletions src/FileCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Minicli\Minicache;

class FileCache
{
protected $cache_dir;
protected $cache_expiry;

/**
* FileCache constructor.
* @param string $cache_dir
* @param int $cache_expiry
*/
public function __construct($cache_dir, $cache_expiry = 60)
{
$this->cache_dir = $cache_dir;
$this->cache_expiry = $cache_expiry;
}

/**
* @param string $unique_identifier
* @return string
*/
public function getCacheFile($unique_identifier)
{
return $this->cache_dir . '/' . md5($unique_identifier) . '.json';
}

/**
* @param string $unique_identifier
* @return null|string
*/
public function getCached($unique_identifier)
{
$cache_file = $this->getCacheFile($unique_identifier);

if (is_file($cache_file)) {
return file_get_contents($cache_file);
}

return null;
}

/**
* @param string $unique_identifier
* @return null|string
*/
public function getCachedUnlessExpired($unique_identifier)
{
$cache_file = $this->getCacheFile($unique_identifier);

// is it still valid?
if (is_file($cache_file) && (time() - filemtime($cache_file) < 60*$this->cache_expiry)) {
return file_get_contents($cache_file);
}

return null;
}

/**
* @param string $content
* @param string $unique_identifier
*/
public function save($content, $unique_identifier)
{
$cache_file = $this->getCacheFile($unique_identifier);

file_put_contents($cache_file, $content);
}
}

0 comments on commit 844be3c

Please sign in to comment.