mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-05-08 08:15:41 +03:00
refacor: improve cache interface (#3492)
* fix: proper typehint on setScope * refactor: type hint setKey() * typehint
This commit is contained in:
parent
f8801d8cb3
commit
caac7f572c
13 changed files with 56 additions and 220 deletions
docs/07_Cache_API
|
@ -1,24 +1,3 @@
|
|||
Create a new file in the `caches/` folder (see [Folder structure](../04_For_Developers/03_Folder_structure.md)).
|
||||
|
||||
The file must be named according to following specification:
|
||||
|
||||
* It starts with the type
|
||||
* The file name must end with 'Cache'
|
||||
* The file type must be PHP, written in small letters (seriously!) ".php"
|
||||
|
||||
**Examples:**
|
||||
|
||||
Type | Filename
|
||||
-----|---------
|
||||
File | FileCache.php
|
||||
MySQL | MySQLCache.php
|
||||
|
||||
The file must start with the PHP tags and end with an empty line. The closing tag `?>` is [omitted](http://php.net/basic-syntax.instruction-separation).
|
||||
|
||||
Example:
|
||||
|
||||
```PHP
|
||||
<?PHP
|
||||
// PHP code here
|
||||
// This line is empty (just imagine it!)
|
||||
```
|
||||
See `NullCache` and `SQLiteCache` for examples.
|
|
@ -1,67 +1,18 @@
|
|||
The `CacheInterface` interface defines functions that need to be implemented. To create a new cache that implements `CacheInterface` you must implement following functions:
|
||||
See `CacheInterface`.
|
||||
|
||||
* [loadData](#the-loaddata-function)
|
||||
* [saveData](#the-savedata-function)
|
||||
* [getTime](#the-gettime-function)
|
||||
* [purgeCache](#the-purgecache-function)
|
||||
```php
|
||||
interface CacheInterface
|
||||
{
|
||||
public function setScope(string $scope): void;
|
||||
|
||||
Find a [template](#template) at the end of this file.
|
||||
public function setKey(array $key): void;
|
||||
|
||||
# Functions
|
||||
public function loadData();
|
||||
|
||||
## The `loadData` function
|
||||
public function saveData($data): void;
|
||||
|
||||
This function loads data from the cache and returns the data in the same format provided to the [saveData](#the-savedata-function) function.
|
||||
public function getTime(): ?int;
|
||||
|
||||
```PHP
|
||||
loadData(): mixed
|
||||
```
|
||||
|
||||
## The `saveData` function
|
||||
|
||||
This function stores the given data into the cache and returns the object instance.
|
||||
|
||||
```PHP
|
||||
saveData(mixed $data): self
|
||||
```
|
||||
|
||||
## The `getTime` function
|
||||
|
||||
## The `purgeCache` function
|
||||
|
||||
This function removes any data from the cache that is not within the given duration. The duration is specified in seconds and defines the period between now and the oldest item to keep.
|
||||
|
||||
```PHP
|
||||
purgeCache(int $duration): null
|
||||
```
|
||||
|
||||
# Template
|
||||
|
||||
This is the bare minimum template for a new cache:
|
||||
|
||||
```PHP
|
||||
<?php
|
||||
class MyTypeCache implements CacheInterface {
|
||||
public function loadData(){
|
||||
// Implement your algorithm here!
|
||||
return null;
|
||||
}
|
||||
|
||||
public function saveData($data){
|
||||
// Implement your algorithm here!
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTime(){
|
||||
// Implement your algorithm here!
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function purgeCache($duration){
|
||||
// Implement your algorithm here!
|
||||
}
|
||||
public function purgeCache(int $seconds): void;
|
||||
}
|
||||
// Imaginary empty line!
|
||||
```
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue