andyvanee/synchroversion
Composer 安装命令:
composer require andyvanee/synchroversion
包简介
File-based, versioning cache system
README 文档
README
Synchroversion is designed with the following properties in mind:
- Keep full history of changes (diffs)
- Writing does not block or interfere with reading
- Reading does not block or interfere with writing
- Changes are timestamped
In general, it is built around the assumption that your data is:
- Text-based
- Not changing wildly on every call (so diffs aren't huge)
- Line-oriented (if you want readable diffs)
- Single producer, multiple consumer
- Fetch speed faster than fetch frequency. I would expect that it may blow up if your fetch takes 5 minutes but you run it every 2 minutes.
Some possible use cases might be:
- Storing and versioning a large API call
- Recording changes made to a directory tree
- Adding database versioning (using database dumps)
It does this by storing a log of changes to the content in diff format in
a state directory, and the latest few full versions of the file in a latest
directory. The producer and consumer are never accessing the same file at the
same time.
My initial prototype was written in bash, but this version is in PHP since it's what I'm currently working in. Since the format is completely text file based, it is language agnostic and should be quite simple to implement in other languages as well.
Usage
Install with composer:
composer require andyvanee/synchroversion
new Synchroversion\Synchroversion($root, $path)
Create a new instance, ready for reading or writing. The first argument is the
root directory where all files are stored and the second is the path within
that root you'd like to use. We'll be tracking syslog in this example, so we'll
just name it syslog.
$sync = new Synchroversion\Synchroversion(dirname(__FILE__), 'syslog');
setUmask($umask)
This can be used to set the permissions of files and directories created. The
default umask is 0022, which means world-readable and user-writeable. You may
choose to set this to 0000 if you want the permissions wide open, or 0007
if you want read/write for the user and group but deny permissions to others.
Check out documentation for UNIX umask for more examples.
$sync->setUmask(0007);
latest()
This returns the latest version of the content that has been stored.
$content = $sync->latest();
exec(callable $cb)
To write a version of the content, pass a callable which returns the data to be stored. A version file will be created as well as a diff if there are previous versions.
$sync->exec(function(){ return 'Hello World!'; });
retainState(int $count)
By default, the three latest full versions of the content are kept. This may be adjusted by setting this to less or more. Note that diffs are kept for the entire history, so it is always possible to reconstruct any past state. These are simply kept to review consistency.
// Hoard all the versions! $sync->retainState(10); // Save my disk space! $sync->retainState(1);
State files are cleaned up on every call to exec(). If you'd like to clean up
state files without calling exec, call purgeStateFiles() instead.
$sync->retainState(1); $sync->purgeStateFiles()
Example files
<?php // producer.php require 'vendor/autoload.php'; $sync = new Synchroversion\Synchroversion(dirname(__FILE__), 'syslog'); $sync->exec(function () { return file_get_contents('/var/log/system.log'); });
<?php // consumer.php require 'vendor/autoload.php'; $sync = new Synchroversion\Synchroversion(dirname(__FILE__), 'syslog'); echo $sync->latest();
These two files illustrate a writer and a reader which track the syslog file.
A producer would likely be run regularly via cron or triggered by some other action. It could run anywhere from every minute to once a year - pick what makes sense for your data.
Run the update every 5 seconds:
while [[ true ]]; do php producer.php; sleep 5; done
In another terminal, fetch the latest version:
php consumer.php
Todo
- Playback state files to validate current state
- Playback state files to generate historical states
- Compressed state files
andyvanee/synchroversion 适用场景与选型建议
andyvanee/synchroversion 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 34 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 andyvanee/synchroversion 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 andyvanee/synchroversion 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 34
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-12