中秋节快乐

This commit is contained in:
Alone
2024-09-14 17:00:49 +08:00
parent 610dee88cd
commit dff1b038b1
4286 changed files with 807503 additions and 6 deletions
+58
View File
@@ -0,0 +1,58 @@
# Change Log
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
## UNRELEASED
## 1.1.0
### Added
* Support for PHP 8
### Changed
* Use `League\Flysystem\FilesystemInterface` instead of concrete `League\Flysystem\Filesystem` class
## 1.0.0
* No changes since 0.4.0
## 0.4.0
### Added
* Support for the new `TaggableCacheItemPoolInterface`.
* Support for PSR-16 SimpleCache
### Changed
* The behavior of `CacheItem::getTags()` has changed. It will not return the tags stored in the cache storage.
### Removed
* `CacheItem::getExpirationDate()`. Use `CacheItem::getExpirationTimestamp()`
* `CacheItem::getTags()`. Use `CacheItem::getPreviousTags()`
* `CacheItem::addTag()`. Use `CacheItem::setTags()`
## 0.3.3
### Fixed
* Race condition in `fetchObjectFromCache`.
## 0.3.2
### Changed
* Using `Filesystem::update` instead of `Filesystem::delete` and `Filesystem::write`.
## 0.3.1
### Added
* Add ability to change cache path in FilesystemCachePool
## 0.3.0
* No changelog before this version
+213
View File
@@ -0,0 +1,213 @@
<?php
/*
* This file is part of php-cache organization.
*
* (c) 2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Cache\Adapter\Filesystem;
use Cache\Adapter\Common\AbstractCachePool;
use Cache\Adapter\Common\Exception\InvalidArgumentException;
use Cache\Adapter\Common\PhpCacheItem;
use League\Flysystem\FileExistsException;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\FilesystemInterface;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class FilesystemCachePool extends AbstractCachePool
{
/**
* @type FilesystemInterface
*/
private $filesystem;
/**
* The folder should not begin nor end with a slash. Example: path/to/cache.
*
* @type string
*/
private $folder;
/**
* @param FilesystemInterface $filesystem
* @param string $folder
*/
public function __construct(FilesystemInterface $filesystem, $folder = 'cache')
{
$this->folder = $folder;
$this->filesystem = $filesystem;
$this->filesystem->createDir($this->folder);
}
/**
* @param string $folder
*/
public function setFolder($folder)
{
$this->folder = $folder;
}
/**
* {@inheritdoc}
*/
protected function fetchObjectFromCache($key)
{
$empty = [false, null, [], null];
$file = $this->getFilePath($key);
try {
$data = @unserialize($this->filesystem->read($file));
if ($data === false) {
return $empty;
}
} catch (FileNotFoundException $e) {
return $empty;
}
// Determine expirationTimestamp from data, remove items if expired
$expirationTimestamp = $data[2] ?: null;
if ($expirationTimestamp !== null && time() > $expirationTimestamp) {
foreach ($data[1] as $tag) {
$this->removeListItem($this->getTagKey($tag), $key);
}
$this->forceClear($key);
return $empty;
}
return [true, $data[0], $data[1], $expirationTimestamp];
}
/**
* {@inheritdoc}
*/
protected function clearAllObjectsFromCache()
{
$this->filesystem->deleteDir($this->folder);
$this->filesystem->createDir($this->folder);
return true;
}
/**
* {@inheritdoc}
*/
protected function clearOneObjectFromCache($key)
{
return $this->forceClear($key);
}
/**
* {@inheritdoc}
*/
protected function storeItemInCache(PhpCacheItem $item, $ttl)
{
$data = serialize(
[
$item->get(),
$item->getTags(),
$item->getExpirationTimestamp(),
]
);
$file = $this->getFilePath($item->getKey());
if ($this->filesystem->has($file)) {
// Update file if it exists
return $this->filesystem->update($file, $data);
}
try {
return $this->filesystem->write($file, $data);
} catch (FileExistsException $e) {
// To handle issues when/if race conditions occurs, we try to update here.
return $this->filesystem->update($file, $data);
}
}
/**
* @param string $key
*
* @throws InvalidArgumentException
*
* @return string
*/
private function getFilePath($key)
{
if (!preg_match('|^[a-zA-Z0-9_\.! ]+$|', $key)) {
throw new InvalidArgumentException(sprintf('Invalid key "%s". Valid filenames must match [a-zA-Z0-9_\.! ].', $key));
}
return sprintf('%s/%s', $this->folder, $key);
}
/**
* {@inheritdoc}
*/
protected function getList($name)
{
$file = $this->getFilePath($name);
if (!$this->filesystem->has($file)) {
$this->filesystem->write($file, serialize([]));
}
return unserialize($this->filesystem->read($file));
}
/**
* {@inheritdoc}
*/
protected function removeList($name)
{
$file = $this->getFilePath($name);
$this->filesystem->delete($file);
}
/**
* {@inheritdoc}
*/
protected function appendListItem($name, $key)
{
$list = $this->getList($name);
$list[] = $key;
return $this->filesystem->update($this->getFilePath($name), serialize($list));
}
/**
* {@inheritdoc}
*/
protected function removeListItem($name, $key)
{
$list = $this->getList($name);
foreach ($list as $i => $item) {
if ($item === $key) {
unset($list[$i]);
}
}
return $this->filesystem->update($this->getFilePath($name), serialize($list));
}
/**
* @param $key
*
* @return bool
*/
private function forceClear($key)
{
try {
return $this->filesystem->delete($this->getFilePath($key));
} catch (FileNotFoundException $e) {
return true;
}
}
}
+22
View File
@@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2015 Aaron Scherer, Tobias Nyholm
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+45
View File
@@ -0,0 +1,45 @@
# Filesystem PSR-6 Cache pool
[![Gitter](https://badges.gitter.im/php-cache/cache.svg)](https://gitter.im/php-cache/cache?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![Latest Stable Version](https://poser.pugx.org/cache/filesystem-adapter/v/stable)](https://packagist.org/packages/cache/filesystem-adapter)
[![codecov.io](https://codecov.io/github/php-cache/filesystem-adapter/coverage.svg?branch=master)](https://codecov.io/github/php-cache/filesystem-adapter?branch=master)
[![Total Downloads](https://poser.pugx.org/cache/filesystem-adapter/downloads)](https://packagist.org/packages/cache/filesystem-adapter)
[![Monthly Downloads](https://poser.pugx.org/cache/filesystem-adapter/d/monthly.png)](https://packagist.org/packages/cache/filesystem-adapter)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
This is a PSR-6 cache implementation using Filesystem. It is a part of the PHP Cache organisation. To read about
features like tagging and hierarchy support please read the shared documentation at [www.php-cache.com](http://www.php-cache.com).
This implementation is using the excellent [Flysystem](http://flysystem.thephpleague.com/).
### Install
```bash
composer require cache/filesystem-adapter
```
### Use
To create an instance of `FilesystemCachePool` you need to configure a `Filesystem` and its adapter.
```php
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Cache\Adapter\Filesystem\FilesystemCachePool;
$filesystemAdapter = new Local(__DIR__.'/');
$filesystem = new Filesystem($filesystemAdapter);
$pool = new FilesystemCachePool($filesystem);
```
You can change the folder the cache pool will write to through the `setFolder` setter:
```php
$pool = new FilesystemCachePool($filesystem);
$pool->setFolder('path/to/cache');
```
### Contribute
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/php-cache/cache) or
report any issues you find on the [issue tracker](http://issues.php-cache.com).
+55
View File
@@ -0,0 +1,55 @@
{
"name": "cache/filesystem-adapter",
"type": "library",
"description": "A PSR-6 cache implementation using filesystem. This implementation supports tags",
"keywords": [
"cache",
"psr-6",
"filesystem",
"tag"
],
"homepage": "http://www.php-cache.com/en/latest/",
"license": "MIT",
"authors": [
{
"name": "Aaron Scherer",
"email": "aequasi@gmail.com",
"homepage": "https://github.com/aequasi"
},
{
"name": "Tobias Nyholm",
"email": "tobias.nyholm@gmail.com",
"homepage": "https://github.com/nyholm"
}
],
"require": {
"php": "^5.6 || ^7.0 || ^8.0",
"cache/adapter-common": "^1.0",
"league/flysystem": "^1.0",
"psr/cache": "^1.0",
"psr/simple-cache": "^1.0"
},
"provide": {
"psr/cache-implementation": "^1.0",
"psr/simple-cache-implementation": "^1.0"
},
"require-dev": {
"cache/integration-tests": "^0.16",
"phpunit/phpunit": "^5.7.21"
},
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
}
},
"autoload": {
"psr-4": {
"Cache\\Adapter\\Filesystem\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev",
"prefer-stable": true
}