承接 tiagogouvea/simple-php-cache 相关项目开发

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

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

tiagogouvea/simple-php-cache

Composer 安装命令:

composer require tiagogouvea/simple-php-cache

包简介

Simple PHP Cache class using file system

README 文档

README

Forked from cosenary/Simple-PHP-Cache

About

A light, simple but powerful PHP5 Cache Class which uses the filesystem for caching.

Requirements

  • PHP 5.2.x or higher

Introduction

Basically the caching class stores its data in files in the JSON format. These files will be created if you store data under a Cache name.

If you set a new Cache name with setCache(), a new cache file will be generated. The Cache will store all further data in the new file. The Setter method allows you to switch between the different Cache files.

Quick Start

Add to your composer.json:

{ "require": { "tiagogougea/simple-php-cache": "*" } }

And run "composer update" or just run "composer require tiagogouvea/simple-php-cache".

Setup Cache class

It's not much trouble to setup the Cache.
First create a writable directory cache/ and include the Cache class:

<?php
    require_once 'cache.class.php';
    
    // setup 'default' cache
    $c = new Cache();
?>

Now we've setup the Cache instance and can start caching!

<?php
    // store a string
    $c->store('hello', 'Hello World!');
    
    // generate a new cache file with the name 'newcache'
    $c->setCache('newcache');
    
    // store an array
    $c->store('movies', array(
      'description' => 'Movies on TV',
      'action' => array(
        'Tropic Thunder',
        'Bad Boys',
        'Crank'
      )
    ));
    
    // get cached data by its key
    $result = $c->retrieve('movies');
    
    // display the cached array
    echo '<pre>';
    print_r($result);
    echo '<pre>';
    
    // grab array entry
    $description = $result['description'];
    
    // switch back to the first cache
    $c->setCache('mycache');
    
    // update entry by simply overwriting an existing key
    $c->store('hello', 'Hello everybody out there!');
    
    // erase entry by its key
    $c->erase('hello');
?>

You can also make use of the Method Chaining feature, introduced in PHP5.
So you can do something like that:

<?php
    $c->setCache('mycache')      // generate new file
      ->store('hello', 'world')  // store data string
      ->retrieve('hello');       // retrieve cached data
?>

Available methods

Setup the Cache

new Cache(<array>/<string>)

string gives you the basic setup.
It's the name of your Cache (standard Cache name is 'default'):

new Cache('YOUR-CACHE-NAME');

array allows you to define multiple optional parameters:

new Cache(array(
  'name'      => 'YOUR-CACHE-NAME',
  'path'      => 'cache/',
  'extension' => '.cache'
));

If you don't define a Cache name with the constructor or the setCache() method, it'll be 'default'.

Store data

store($key, $data, <$expiration>)

  • The key value defines a tag with which the cached data will be associated.
  • The data value can be any type of object (will be serialized).
  • The expiration value allows you to define an expiration time.

To change data you can overwrite it by using the same key identifier.
Beside the data, the Cache will also store a timestamp.

A sample Cache entry looks like this:

{
  "christmas": {
    "time": 1324664631,
    "expire": 28000,
    "data": "s:29:"A great time to bake cookies.";" // serialized
  }
}

Retrieve data

retrieve($key, <$timestamp>)

Get particular cached data by its key.
To retrieve the timestamp of a key, set the second parameter to true.

retrieveAll(<$meta>)

This allows you retrieve all the cached data at once. You get the meta data by setting the $meta argument to true.

Erase data

For erasing cached data are these three methods available:

  • erase($key) Erases a single entry by its key.
  • eraseAll() Erases all entries from the Cache file.
  • eraseExpired() Erases all expired entries.
<?php
    // returns the count of erased entries  
    echo $c->eraseExpired() . ' expired items erased!';
?>

Check cached data

isCached($key)

Check whether any data is associated with the given key.
Returns true or false.

Set Cache name

setCache($name)

If you want to switch to another Cache or create a new one, then use this method to set a new Cache name.

Set Cache path

setCachePath($path)

The path to the Cache folder must end with a backslash: my_path_to_the_cache_folder/

Get Cache file path

getCacheDir()

The method returns the path to your current Cache file (the Cache name is always sh1 encoded):

cache/7505d64a54e061b7acd54ccd58b49dc43500b635.cache

Benchmarks

If you've done one, please let me know.

History

Upcoming: Simple Cache 2.0
Implementation of an internal "soft cache", hash-sum handling and the switch to serialization. Thanks @dariushha for his contribution!

Simple Cache 1.6 - 04/01/2014

  • update Updated docs.
  • bug Fixed retrieveAll() method to unserialize data.

Simple Cache 1.5 - 01/01/2014

  • feature added serialize / unserialize to store any kind of data.

Simple Cache 1.4 - 08/09/2013

  • bug Fixed loading file twice in store() method.
  • bug Fixed retrieve() method - made it fail safe (thanks @dariushha).

Simple Cache 1.3 - 28/02/2013

  • update Updated docs for the added retrieveAll() method.
  • feature Added retrieveAll() method (thanks @rpnzl).

Simple Cache 1.2 - 09/05/2012

  • update Formatted code
  • bug Fixed isCached() method so that it works as expected (thanks @TigerWolf).

Simple Cache 1.1 - 01/01/2012

  • change The extension config has to start now with a dot.
  • feature Added expiration handling to the store() method
  • feature Added the methods eraseExpired() and eraseAll()
  • feature Added method to make sure that a writable directory exists

Simple Cache 1.0 - 29/12/2011

  • release First public version
  • feature Added timestamp option to the retrieve() method

Simple Cache 0.9 - 25/12/2011

  • update Added Quick Start guide to the documentation
  • feature Added Method Chaining possibility
  • bug Fixed constructor configuration string/array handling

Simple Cache 0.8 - 24/12/2011

  • release First internal beta version (tested)
  • feature Added Setter and Getter methods
  • update Detailed documentation

Simple Cache 0.5 - 22/12/2011

  • release First internal alpha version
  • update Small documentation

Credits

Copyright (c) 2011-2013 - Programmed by Christian Metz / @cosenary
Released under the BSD License.

tiagogouvea/simple-php-cache 适用场景与选型建议

tiagogouvea/simple-php-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 4, 最近一次更新时间为 2015 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 104
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2015-08-15