PHPackages                             hi-man/localcache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. hi-man/localcache

ActiveLibrary[Caching](/categories/caching)

hi-man/localcache
=================

provides a cache with local and remote cache

v0.0.9(5y ago)023MITPHPPHP &gt;=7.0.0CI failing

Since Aug 8Pushed 5y ago1 watchersCompare

[ Source](https://github.com/hi-man/localcache)[ Packagist](https://packagist.org/packages/hi-man/localcache)[ RSS](/packages/hi-man-localcache/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (2)Versions (10)Used By (0)

LocalCache
==========

[](#localcache)

[![Build Status](https://camo.githubusercontent.com/6e2be475dd364061013d05a583c250c83cac44a48e05ddc45493deda9238dca2/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f68692d6d616e2f6c6f63616c63616368652e706e67)](https://travis-ci.org/hi-man/localcache)

now, let's back to our classroom, professor is talking about the architecture of `cpu`, just like the software your are creating，`Redis` is the `L2` cache, and `Yac` is the `L1` cache for your software, and `LocalCache` is the cache manager of `Redis` and `Yac`.

so, if your redis server is busying, and your data does not change frequently, `LocalCache` is the right man you are finding.

> If I have seen further, it is by standing on the shoulders of giants.

thanks to [phpredis](https://github.com/phpredis/phpredis) and [yac](https://github.com/laruence/yac)

- [Feature](#feature)
- [Usage](#usage)
- [Requirement](#requirement)
- [Install](#install)
- [testing](#testing)
- [Classes](#classes)
    - [CachePoolService](#cachepoolservice)
        - [initCacheInstance](#initcacheinstance)
        - [getCacheInstance](#getcacheinstance)
        - [setCacheValue](#setcachevalue)
        - [getCacheValue](#getcachevalue)
        - [deleteByKey](#deletebykey)
    - [CacheService](#cacheservice)
        - [getConfigByConnection](#getconfigbyconnection)
        - [logException](#logexception)
        - [setCacheValue](#setcachevalue-1)
        - [getCacheValue](#getcachevalue-1)
        - [deleteByKey](#deletebykey-1)
    - [LocalCache](#localcache)
        - [Construct](#construct)
        - [select](#select)
        - [get](#get)
        - [set](#set)
        - [delete / unlink](#delete--unlink)
        - [expire](#expire)
        - [clear](#clear)
        - [hDel / hGetAll / hMSet / hMGet / hSet / hGet](#hdel--hgetall--hmset--hmget--hset--hget)
        - [setLocalCacheTimeout](#setlocalcachetimeout)
        - [getLocalCacheTimeout](#getlocalcachetimeout)

Feature
=======

[](#feature)

- a redis client with retry mechanism
- reduce redis requests by caching data in memory
- support redis connection pool
- more graceful exception handler
- disable cache on demands

Usage
=====

[](#usage)

1. extend class `CacheService` (recommend)
2. use static funcitons in `CachePoolService`
3. use `LocalCache` class directly

Requirement
===========

[](#requirement)

- PHP 7.0+
- phpredis
- yac

Install
=======

[](#install)

```
composer require 'hi-man/localcache'

```

testing
=======

[](#testing)

```
composer test

```

Classes
=======

[](#classes)

CachePoolService
----------------

[](#cachepoolservice)

---

pool of `LocalCache`, can be called statically

### initCacheInstance

[](#initcacheinstance)

initialize `LocalCache`

### getCacheInstance

[](#getcacheinstance)

get instance of `LocalCache` by connection name

### setCacheValue

[](#setcachevalue)

set cache value

### getCacheValue

[](#getcachevalue)

get cache value

### deleteByKey

[](#deletebykey)

delete from cache by key

CacheService
------------

[](#cacheservice)

---

abstract class to use CachePoolService more productive

### getConfigByConnection

[](#getconfigbyconnection)

**MUST implemented** get redis configuration by connection identifier

### logException

[](#logexception)

**MUST implemented** exception handler

### setCacheValue

[](#setcachevalue-1)

wrapper of `CachePoolService::setCacheValue`

### getCacheValue

[](#getcachevalue-1)

wrapper of `CachePoolService::getCacheValue`

### deleteByKey

[](#deletebykey-1)

wrapper of `CachePoolService::deleteByKey`

LocalCache
----------

[](#localcache-1)

---

provide a local cache between application and redis server

### Construct

[](#construct)

```
$lc = new LocalCache(
  '127.0.01' /* redis host */,
  'Yac prefix' /* yac prefix, empty prefix will disable yac, default value is empty, max length is 20 */,
  6379 /* redis port, default value is 6379 */,
  3 /* redis connection timeout, in seconds, default value is 3 */,
  500000 /* redis retry interval, in microseconds, default value is 500000 */,
  3 /* redis read timeout, default value is 3 */,
  3 /* max retry, default value is 3 */,
  0 /* redis reserved, default value is 0 */
);
```

### select

[](#select)

the same as redis command `select`, but not really issue a command request.

```
$lc->select(0 /* redis database index */);
```

### get

[](#get)

the same as redis command `get`, use yac cache value first, then issue a command request if cache is missing.

```
$lc->get(
  'key' /* redis item key */,
  'default value' /* default value if the key does not exists */
);
```

### set

[](#set)

the same as redis command `set`, reset yac cache value

```
$lc->set(
    'key',      /* redis item key */
    'value',    /* value to store */
    3           /* ttl */
    'default value' /* default value if the key does not exists */
);
```

### delete / unlink

[](#delete--unlink)

the same as redis command `delete` or `unlink`, also delete yac cache

```
$lc->delete(
    'key',      /* redis item key */
);

$lc->unlink(
    'key',      /* redis item key */
);
```

### expire

[](#expire)

the same as redis command `expire`, also reset yac cache expire time

```
$lc->expire('key' /* redis item key */, 3 /* expire time in seconds */);
```

### clear

[](#clear)

the same as redis command `flushdb`, but flush **all** yac cache

```
$lc->clear();
```

### hDel / hGetAll / hMSet / hMGet / hSet / hGet

[](#hdel--hgetall--hmset--hmget--hset--hget)

the same as redis command

### setLocalCacheTimeout

[](#setlocalcachetimeout)

set yac cache timeout, **set the right value for your scenario**. cache invalidation is a big concept to deal with.

```
$lc->setLocalCacheTimeout(
    'key',      /* redis item key */
    'value',    /* value to store */
    3           /* ttl */
    'default value' /* default value if the key does not exists */
);
```

### getLocalCacheTimeout

[](#getlocalcachetimeout)

get yac cache timeout

```
$lc->getLocalCacheTimeout();
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~59 days

Recently: every ~112 days

Total

9

Last Release

1995d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4e0b86b1b16b5a13ee9f1db00f5c84d818eec0b2a30b8bae3ffbfa674a0b489f?d=identicon)[hi-man](/maintainers/hi-man)

---

Top Contributors

[![hi-man](https://avatars.githubusercontent.com/u/899605?v=4)](https://github.com/hi-man "hi-man (23 commits)")

---

Tags

cachecomposer-packagelocalcachephpphpredisredisyac

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hi-man-localcache/health.svg)

```
[![Health](https://phpackages.com/badges/hi-man-localcache/health.svg)](https://phpackages.com/packages/hi-man-localcache)
```

###  Alternatives

[laminas/laminas-cache

Caching implementation with a variety of storage options, as well as codified caching strategies for callbacks, classes, and output

1076.9M130](/packages/laminas-laminas-cache)[cache/adapter-common

Common classes for PSR-6 adapters

11124.4M38](/packages/cache-adapter-common)[cache/filesystem-adapter

A PSR-6 cache implementation using filesystem. This implementation supports tags

705.8M82](/packages/cache-filesystem-adapter)[cache/array-adapter

A PSR-6 cache implementation using a php array. This implementation supports tags

548.3M151](/packages/cache-array-adapter)[cache/redis-adapter

A PSR-6 cache implementation using Redis (PhpRedis). This implementation supports tags

523.9M27](/packages/cache-redis-adapter)[cache/simple-cache-bridge

A PSR-6 bridge to PSR-16. This will make any PSR-6 cache compatible with SimpleCache.

423.1M27](/packages/cache-simple-cache-bridge)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
