承接 gregwar/cache 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gregwar/cache

Composer 安装命令:

composer require gregwar/cache

包简介

A lightweight file-system cache system

README 文档

README

Build status paypal

This is a lightweight cache system based on file and directories.

Usage

Step 1: Install it

Via composer:

{
    "require": {
        "gregwar/cache": "1.0.*"
    }
}

Or with a clone of the repository:

git clone https://github.com/Gregwar/Cache.git

Or downloading it:

Step 2: Setup the rights

You need your PHP script to have access to the cache directory, you can for instance create a cache directory (be sure the web server can write it):

mkdir cache

Step 3: Access the cache

To access the cache, you can do like this:

<?php

include('vendor/autoload.php'); // If using composer

use Gregwar\Cache\Cache;

$cache = new Cache;
$cache->setCacheDirectory('cache'); // This is the default

// If the cache exists, this will return it, else, the closure will be called
// to create this image
$data = $cache->getOrCreate('red-square.png', array(), function($filename) {
    $i = imagecreatetruecolor(100, 100);
    imagefill($i, 0, 0, 0xff0000);
    imagepng($i, $filename);
});

header('Content-type: image/png');
echo $data;

This will render a red square. If the cache file (which will look like `cache/r/e/d/-/s/red-square.png') exists, it will be read, else, the closure will be called in order to create the cache file.

API

You can use the following methods:

  • setCacheDirectory($directory): sets the cache directory (see below).
  • setActualCacheDirectory($directory): sets the actual cache directory (see below).
  • exists($filename, $conditions = array()): check that the $filename file exists in the cache, checking the conditions (see below).
  • check($filename, $conditions = array()): alias for exists.
  • getCacheFile($filename, $actual = false, $mkdir = false): gets the cache file. If the $actual flag is true, the actual cache file name will be returned (see below), if the $mkdir flag is true, the cache file directories tree will be created.
  • set($filename, $contents): write contents to $filename cache file.
  • write($filename, $contents): alias for set()
  • get($filename, $conditions = array()): if the cache file for $filename exists, contents will be returned, else, NULL will be returned.
  • setPrefixSize($prefixSize): sets the prefix size for directories, default is 5. For instance, the cache file for helloworld.txt, will be 'h/e/l/l/o/helloworld.txt.
  • setDirectoryMode($directoryMode): sets the directory mode when creating directories, default is 0755. Does not affect any directories previously created.
  • getOrCreate($filename, $conditions = array(), $function, $file = false): this will check if the $filename cache file exists and verifies $conditions (see below). If the cache file is OK, it will return its contents. Else, it will call the $function, passing it the target file, this function can write the file given in parameter or just return data. Then, cache data will be returned. If $file flag is set, the cache file name will be returned instead of file data.

Note: consider using an hash for the $filename cache file, to avoid special characters.

Conditions

You can use conditions to manage file expirations on the cache, there is two way of expiring:

  • Using max-age, in seconds, to set the maximum age of the file
  • Using younger-than, by passing another file, this will compare the modification date and regenerate the cache if the given file is younger.

For instance, if you want to uppercase a file:

<?php

use Gregwar\Cache\Cache;

$cache = new Cache;

$data = $cache->getOrCreate('uppercase.txt',
    array(
        'younger-than' => 'original.txt'
    ),
    function() {
        echo "Generating file...\n";
        return strtoupper(file_get_contents('original.txt'));
});

echo $data;

This will be create the uppercase.txt cache file by uppercasing the original.txt if the cache file does not exists or if the original.txt file is more recent than the cache file.

For instance:

php uppercase.php  # Will generate the cache file
php uppercase.php  # Will not generate the cache file
touch original.txt # Sets the last modification time to now
php uppercase.php  # Will re-generate the cache file

Cache directory and actual cache directory

In some cases, you'll want to get the cache file name. For instance, if you're caching images, you'll want to give a string like cache/s/o/m/e/i/someimage.png to put it into an <img> tag. This can be done by passing the $file argument to the getOrCreate to true, or directly using getCacheFile method (see above).

However, the visible cache directory of your users is not the same as the absolute path you want to access. To do that, you can set both the cache directory and the actual cache directory.

The cache directory is the prefix visible by the users (for instance: cache/s/o/m/e/i/someimage.png), and the actual cache directory is the prefix to use to actually access to the image (for instance: /var/www/somesite/cache/s/o/m/e/i/someimage.png). This way, the file will be accessed using absolute path and the cache file returned will directly be usable for your user's browsers.

License

This repository is under the MIT license, have a look at the LICENCE file.

gregwar/cache 适用场景与选型建议

gregwar/cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.6M 次下载、GitHub Stars 达 108, 最近一次更新时间为 2013 年 10 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cache」 「file-system」 「caching」 「system」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gregwar/cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gregwar/cache 我们能提供哪些服务?
定制开发 / 二次开发

基于 gregwar/cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.6M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 112
  • 点击次数: 17
  • 依赖项目数: 18
  • 推荐数: 0

GitHub 信息

  • Stars: 108
  • Watchers: 5
  • Forks: 30
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-10-06