2017 © Pedro Peláez
 

library mcounter

Very simple lib for memcached based counters.

image

dorantor/mcounter

Very simple lib for memcached based counters.

  • Wednesday, February 14, 2018
  • by dorantor
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2,093 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 6 Versions
  • 22 % Grown

The README.md

mcounter

Dead simple memcached counter., (*1)

Installation

composer require dorantor/mcounter

Usage

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

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:, (*3)

$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:, (*4)

$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.:, (*5)

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.:, (*6)

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

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

In case you need to reset counter you have two options:, (*8)

// 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., (*9)

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

$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., (*11)

The Versions

14/02 2018

dev-master

9999999-dev https://github.com/dorantor/mcounter/

Very simple lib for memcached based counters.

  Sources   Download

MIT

The Requires

  • php ^5.6|^7.0
  • ext-memcached *

 

memcached counter cipher

14/02 2018

v1.2.0

1.2.0.0 https://github.com/dorantor/mcounter/

Very simple lib for memcached based counters.

  Sources   Download

MIT

The Requires

  • php ^5.6|^7.0
  • ext-memcached *

 

memcached counter cipher

13/02 2018

v1.1.1

1.1.1.0 https://github.com/dorantor/mcounter/

Very simple lib for memcached based counters.

  Sources   Download

MIT

The Requires

  • php ^5.6|^7.0
  • ext-memcached *

 

memcached counter cipher

13/02 2018

v1.1.0

1.1.0.0 https://github.com/dorantor/mcounter/

Very simple lib for memcached based counters.

  Sources   Download

MIT

The Requires

  • php ^5.6|^7.0
  • ext-memcached *

 

memcached counter cipher

01/09 2017

v1.0.1

1.0.1.0 https://github.com/dorantor/mcounter/

Very simple lib for memcached based counters.

  Sources   Download

MIT

The Requires

  • php ^5.6|^7.0
  • ext-memcached *

 

memcached counter cipher

31/08 2017

v1.0.0

1.0.0.0 https://github.com/dorantor/mcounter/

Very simple lib for memcached based counters.

  Sources   Download

MIT

The Requires

  • php ^5.6|^7.0
  • ext-memcached *

 

memcached counter cipher