GithubHelp home page GithubHelp logo

cache-db's Introduction

Yii Cache Library - DB Handler


Latest Stable Version Total Downloads Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

This package implements database-based PSR-16 cache.

Support databases:

Packages PHP Versions CI-Actions
[db-mssql] 8.0 - 8.1 2017 - 2019 mssql
[db-mysql] 8.0 - 8.1 5.7 - 8.0 mysql
[db-oracle] 8.0 - 8.1 11c - 12c oracle
[db-pgsql] 8.0 - 8.1 9.0 - 14.0 pgsql
[db-sqlite] 8,0 - 8.1 3:latest sqlite

Requirements

  • PHP 8.0 or higher.
  • PDO PHP extension.

Installation

The package could be installed with composer:

composer require yiisoft/cache-db --prefer-dist

Configuration

When creating an instance of \Yiisoft\Cache\Db\DbCache, you must pass an instance of the database connection:

$cache = new \Yiisoft\Cache\Db\DbCache($db, $table, $gcProbability);
  • $db (\Yiisoft\Db\Connection\ConnectionInterface) - The database connection instance.
  • $table (string) - The name of the database table to store the cache data. Defaults to "cache".
  • $gcProbability (int) - The probability (parts per million) that garbage collection (GC) should be performed when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance. This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.

General usage

The package does not contain any additional functionality for interacting with the cache, except those defined in the PSR-16 interface.

$cache = new \Yiisoft\Cache\Db\DbCache($db);
$parameters = ['user_id' => 42];
$key = 'demo';

// try retrieving $data from cache
$data = $cache->get($key);

if ($data === null) {
    // $data is not found in cache, calculate it from scratch
    $data = calculateData($parameters);
    
    // store $data in cache for an hour so that it can be retrieved next time
    $cache->set($key, $data, 3600);
}

// $data is available here

In order to delete value you can use:

$cache->delete($key);
// Or all cache
$cache->clear();

To work with values in a more efficient manner, batch operations should be used:

  • getMultiple()
  • setMultiple()
  • deleteMultiple()

This package can be used as a cache handler for the Yii Caching Library.

Additional logging

In order to log details about failures you may set a logger instance. It should be Psr\Log\LoggerInterface::class. For example, you can use yiisoft\Log:

$cache = new \Yiisoft\Cache\Db\DbCache($db, $table, $gcProbability);
$cache->setLogger(new \Yiisoft\Log\Logger());

This allows you to log cache operations, when ocurring errors, etc.

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:

./vendor/bin/roave-infection-static-analysis-plugin

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

License

The Yii Cache Library - DB Handler is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

cache-db's People

Contributors

darkdef avatar devanych avatar samdark avatar sankaest avatar terabytesoftw avatar vjik avatar xepozz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.