lefuturiste/localstorage

类似于redis的键/值JSON本地存储

v1.4.2 2022-05-03 07:59 UTC

This package is auto-updated.

Last update: 2024-08-30 02:00:31 UTC


README

Build Status

如果您需要在PHP应用程序中实现磁盘上的本地存储/cache键值,则此库可能很有用。

安装

您可以使用composer轻松安装localstorage库

composer require lefuturiste/localstorage

使用方法

所有方法都在Lefuturiste\LocalStorage\LocalStorage类中

这是此库的一个简单快速示例。

<?php
require 'vendor/autoload.php';

// simple key value store
$localStorage = new Lefuturiste\LocalStorage\LocalStorage();
$localStorage->set('my-key', 'my-value'); // set a value
$localStorage->set('my-key', ['object' => ['is_complex' => true, 'number' => 1]]); // all values are json encoded so you can save array with string, int, float, boolean and null values
$localStorage->save(); // this will write the file on disk, don't forget to call it when you mutate the state!
$localStorage->get('my-key'); // retrieve the value (this will decode the JSON)
$localStorage->has('my-key'); // will return true
$localStorage->has('unknown-key'); // will return false
$localStorage->del('my-key'); // yes it does what you think it will do
$localStorage->clear(); // you can remove all the keys using the clear() method
$localStorage->unlinkStorage(); // you can complety remove the .json file on the disk

// duration management
// by default all the keys are saved with the date of the creating, so you can if you want, delete all the keys olden than a specified duration.
$localStorage->deleteOlderThan(\Carbon\CarbonInterval::seconds(10));

测试

所有测试都在test文件夹中

vendor/bin/phpunit test