PHPackages                             dorantor/mcounter - 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. dorantor/mcounter

ActiveLibrary[Caching](/categories/caching)

dorantor/mcounter
=================

Very simple lib for memcached based counters.

v1.3.0(4y ago)09.1k1MITPHPPHP ^5.6|^7.0

Since Aug 31Pushed 4y ago1 watchersCompare

[ Source](https://github.com/dorantor/mcounter)[ Packagist](https://packagist.org/packages/dorantor/mcounter)[ Docs](https://github.com/dorantor/mcounter/)[ RSS](/packages/dorantor-mcounter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)DependenciesVersions (7)Used By (0)

mcounter
========

[](#mcounter)

Dead simple memcached counter.

Installation
------------

[](#installation)

```
composer require dorantor/mcounter
```

Usage
-----

[](#usage)

NB! There are only abstract classes, because it's meant to be extended.

```
class MyCounter extends \Dorantor\AbstractCounter
{
    /**
     * Method for building cache key based on $item value
     *
     * @return string
     */
    protected function getKey()
    {
        return 'myCounter' . (int) $this->item->id;
    }
}
```

And in your code:

```
$client = new Memcached();
// .. client setup
//
// basically, $client creation is up to you.
// Most probably you already created one earlier, so just reuse it here.
$counter = new MyCounter($user, $client);
if ($counter->value() < 100) {
    $counter->inc();
}
```

By default it's set to never expire. But if you need to use self expiring counter(flag?), you can set third parameter in the constructor:

```
$counter = new MyCounter($user, $client, 3600); // hour, in this case
```

or you can define expiry logic/value inside counter by overriding `getExpiry()` method, p.ex.:

```
protected function getExpiry()
{
    return 3600; // also hour, but this way it's defined inside counter
    // or it could be some logic based on value(s) in $this->item
}
```

*NB!* Expiry is not updated on inc() call. It's default Memcached behaviour. If you need to update expiry use `touch()`, p.ex.:

```
$counter->inc();
$counter->touch();
// or
$counter->incWithTouch();
```

Second option is more convenient but you loose control over `touch()`success/fail.

In case you need to reset counter you have two options:

```
// this will delete counter, so it will be recreated
$counter->delete();

// this will get data from getInitialData() method
// and put it as current counter value
$counter->reload();
```

`delete()` is more viable for cache reset cases, `reload()` - for cases when you need to sync counter with current values from your system if counter is used for caching purposes. For example, if your counter value is `select count(*) from tablename`.

*Important note.* You will have to use binary protocol in memcached. For example, it could be enabled this way:

```
$client->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_IGBINARY);
$client->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
```

But you will also need a binary serializer installed, as you can see. Igbinary in my example.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity62

Established project with proven stability

 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 ~285 days

Recently: every ~357 days

Total

6

Last Release

1754d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2329a22e1ce0a43c84d94c124dfacb2d16ef842b24348fa0f3a276785ed4bed2?d=identicon)[dorantor](/maintainers/dorantor)

---

Top Contributors

[![dorantor](https://avatars.githubusercontent.com/u/391611?v=4)](https://github.com/dorantor "dorantor (12 commits)")

---

Tags

ciphermemcachedcounter

### Embed Badge

![Health badge](/badges/dorantor-mcounter/health.svg)

```
[![Health](https://phpackages.com/badges/dorantor-mcounter/health.svg)](https://phpackages.com/packages/dorantor-mcounter)
```

###  Alternatives

[tedivm/stash

The place to keep your cache.

9824.8M124](/packages/tedivm-stash)[awssat/laravel-visits

Laravel Redis visits counter for Eloquent models

975163.6k2](/packages/awssat-laravel-visits)[tedivm/stash-bundle

Incorporates the Stash caching library into Symfony.

841.4M16](/packages/tedivm-stash-bundle)[sabre/cache

Simple cache abstraction layer implementing PSR-16

541.2M3](/packages/sabre-cache)[clickalicious/phpmemadmin

phpMemAdmin - Bringing Memcached to the web

5076.0k](/packages/clickalicious-phpmemadmin)[cache/memcached-adapter

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

161.9M14](/packages/cache-memcached-adapter)

PHPackages © 2026

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