benjamindean/one-line-apc 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

benjamindean/one-line-apc

Composer 安装命令:

composer require benjamindean/one-line-apc

包简介

Dead simple wrapper class for APC and APCu which capable of setting and getting cached data in one line.

README 文档

README

Dead simple wrapper class for APC and APCu which capable of setting and getting cached data from a callback or a variable in one single line.

Requirements

  • PHP 5.3 or later
  • php-apc extension installed

Installation

Install with Composer:

$ composer require benjamindean/one-line-apc

Usage

require 'vendor/autoload.php';
$cache = new OneLineAPC();

By default, this class uses APC. To use APCu just pass it while instantiating the class:

$cache = new OneLineAPC('apcu');

Default TTL is 79200 (22 hours). To change it, call setTtl method:

$cache->setTtl(3600);

OR, specify it individually as a last argument to setCache or cached methods:

$cache->setCache($dataToCache, 'key', 3600);

Cached

The main and the most important method (the reason I created this class) is called cached:

$cache->cached('key', 'functionName');

functionName is the name of some function in your code which returns the data that needs to be cached. Any variable, integer or string will work too. You can also pass function arguments as a third argument here.

In case your function is within some class, pass it as an array of Object and a function name:

$cache->cached('key', array($obj, 'functionName'));

You can find out more about callbacks here.

Examples

Class method

class ReturnData {
    public function fetchData($url) {
        return file_get_contents($url);
    }
}

$obj = new ReturnData();

$apc = new OneLineAPC();
$apc->setTtl(3600);

return $apc->cached('key', array($obj, 'fetchData'), array('http://example.com/'));

Function

function fetchData($url) {
    return file_get_contents($url);
}

$apc = new OneLineAPC();
$apc->setTtl(3600);

return $apc->cached('key', 'fetchData', array('http://example.com/'));

Variable

$something = 'Data to be cached';

$apc = new OneLineAPC();

return $apc->cached('key', $something, false, 3600);

Notes

If apc or apcu is not loaded, this class will generate "Notice", instead of "Fatal Error".

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-04-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固