codedgr/memory

将数据存储在ram、数据库、会话和cookie中。

1.0.1 2018-06-08 08:33 UTC

This package is auto-updated.

Last update: 2024-09-29 01:12:37 UTC


README

这个库可以帮助您在内存、数据库、cookie或会话中存储临时数据。使用它来缓存数据以提高脚本速度。

安装

使用Composer

"require": {
    "codedgr/memory": "~1.0"
}

将数据存储到内存、会话、数据库或cookie中

初始化内存。注意cookie不需要初始化。

Ram::init();
Session::init();
Database::init($pdo);

如果缓存中的键 $key 不存在,则将数据添加到缓存中。如果它已经存在,则不会添加数据,并且函数返回 false

Ram::add($key, $data, $expire);
Session::add($key, $data, $expire);
Database::add($key, $data, $expire);
Cookie::add($key, $data, $expire);

将数据添加到缓存中。如果缓存中的键 $key 已存在,则将其覆盖;如果不存在,则创建。

Ram::set($key, $data, $expire);
Session::set($key, $data, $expire);
Database::set($key, $data, $expire);
Cookie::set($key, $data, $expire);

如果存在,则替换给定的缓存,否则返回 false

Ram::replace($key, $expire, $group);
Session::replace($key, $expire, $group);
Database::replace($key, $expire, $group);
Cookie::replace($key, $expire, $group);

获取缓存对象的值,如果缓存中的键 $key 不存在,则返回 false

为了区分缓存的 false 和不存在的键 $key,您应该对通过引用传递的 $found 进行绝对测试,与 false 进行比较:如果 $found === false,则键不存在。

Ram::get($key, $expire, &$found = null);
Session::get($key, $expire, &$found = null);
Database::get($key, $expire, &$found = null);
Cookie::get($key, $expire, &$found = null);

清除给定键 $key 的缓存中的数据。

Ram::delete($key, $expire);
Session::delete($key, $expire);
Database::delete($key, $expire);
Cookie::delete($key, $expire);

清除所有缓存数据。注意您不能刷新cookie内存。

Ram::flush();
Session::flush();
Database::flush();