gregwar/cache
Composer 安装命令:
composer require gregwar/cache
包简介
A lightweight file-system cache system
README 文档
README
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 forexists.getCacheFile($filename, $actual = false, $mkdir = false): gets the cache file. If the$actualflag is true, the actual cache file name will be returned (see below), if the$mkdirflag is true, the cache file directories tree will be created.set($filename, $contents): write contents to$filenamecache file.write($filename, $contents): alias forset()get($filename, $conditions = array()): if the cache file for$filenameexists, contents will be returned, else,NULLwill be returned.setPrefixSize($prefixSize): sets the prefix size for directories, default is 5. For instance, the cache file forhelloworld.txt, will be'h/e/l/l/o/helloworld.txt.setDirectoryMode($directoryMode): sets the directory mode when creating directories, default is0755. Does not affect any directories previously created.getOrCreate($filename, $conditions = array(), $function, $file = false): this will check if the$filenamecache 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$fileflag 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gregwar/cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony2 Bundle for serving protected files.
repository php library
Type-safe functions for file system operations with proper exception handling
Query caching for Laravel 5
A lightweight file-system cache system
Simple, fast, standalone PHP lib, that helps You to define, if any of your files were modified since last time You check - Is this file FRESH?
统计信息
- 总下载量: 4.6M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 112
- 点击次数: 17
- 依赖项目数: 18
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-10-06