简体   繁体   English

使用到期日期在服务器上缓存 JSON?

[英]Caching JSON on server using expiration date?

This is some working code that I wrote to call a JSON file and cache it on my servers.这是我编写的一些工作代码,用于调用 JSON 文件并将其缓存在我的服务器上。

I'm calling the cached file.我正在调用缓存文件。 If the file exists I use json_decode on it.如果文件存在,我会在其上使用 json_decode。 If the file doesn't exist I then call the JSON and decode that.如果该文件不存在,则调用 JSON 并对其进行解码。 Then after calling the JSON url, I write the contents to the cached file url.然后在调用 JSON url 后,我将内容写入缓存文件 url。

$cache = @file_get_contents('cache/'.$filename.'.txt');

 //check to see if file exists:
if (strlen($cache)<1) {

    // file is empty
    echo '<notcached />';
    $JSON1= @file_get_contents($url);

    $JSON_Data1 = json_decode($JSON1);

    $myfile = fopen('cache/'.$filename.'.txt', "w");
    $put = file_put_contents('cache/'.$filename.'.txt', ($JSON1));

} else {

    //if file doesn't exist:
    $JSON_Data1 = json_decode($cache);
    echo '<cached />';
}

Instead of only using if (strlen($cache)<1) { , is there a way that I can check the age of the $filename.txt and if it's older than 30 days grab the JSON url in the else statement?而不是只使用if (strlen($cache)<1) { ,有没有一种方法可以检查 $filename.txt 的年龄,如果它超过 30 天,则在 else 语句中获取 JSON url?

You can use something like你可以使用类似的东西

$file = 'cache/'.$filename.'.txt';
$modify = filemtime($file);
//check to see if file exists:
if ($modify == false || $modify < strtotime('now -30 day')) {
    // file is empty, or too old
    echo '<notcached />';
} else {
    // Good to use file
    echo '<cached />';
}

filemtime() returns the last modified time of the file, the if statement checks that the file exists ( filemtime returns false if it fails) or the file was last modified more than 30 days ago. filemtime()返回文件的最后修改时间,if 语句检查文件是否存在(如果失败, filemtime返回 false)或文件是否在 30 多天前被最后修改。

OR... to check if the file exists or too old (without warnings)或...检查文件是否存在或太旧(没有警告)

$file = 'cache/'.$filename.'.txt';
if (file_exists($file) == false || filemtime($file) < strtotime('now -30 day')) {
    // file is empty, or too old
    echo '<notcached />';
} else {
    // Good to use file
    echo '<cached />';
}

I've used a simple file cache class in my previous projects, I think that should help you.我在以前的项目中使用了一个简单的文件缓存类,我认为这应该对您有所帮助。 I think it's easy to understand, the cache time is in seconds and the setFilename function cleans the filename in case it contains invalid characters.我认为这很容易理解,缓存时间以秒为单位, setFilename函数会清除文件名,以防它包含无效字符。

<?php

class SimpleFileCache
{
    var $cache_path = 'cache/';
    var $cache_time = 3600;
    var $cache_file;

    function __construct($name)
    {
        $this->setFilename($name);
    }

    function getFilename()
    {
        return $this->cache_file;
    }

    function setFilename($name)
    {
        $this->cache_file = $this->cache_path . preg_replace('/[^0-9a-z\.\_\-]/', '', strtolower($name));
    }

    function isCached()
    {
        return (file_exists($this->cache_file) && (filemtime($this->cache_file) + $this->cache_time >= time()));
    }

    function getData()
    {
        return file_get_contents($this->cache_file);
    }

    function setData($data)
    {
        if (!empty($data)) {
            return file_put_contents($this->cache_file, $data) > 0;
        }
        return false;
    }
}

It can be used like this.它可以像这样使用。

<?php

require_once 'SimpleFileCache.php';

$cache = new SimpleFileCache('cache.json');
if ($cache->isCached()) {
    $json = $cache->getData();
} else {
    $json = json_encode($someData); // set your cache data
    $cache->setData($json);
}

header('Content-type: application/json');
echo $json;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM