Giter Club home page Giter Club logo

mcounter's Introduction

mcounter

Dead simple memcached counter.

Installation

composer require dorantor/mcounter

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.

mcounter's People

Contributors

dorantor avatar

Watchers

 avatar

Forkers

ivancoff

mcounter's Issues

Initial increment

Current behavior
When incrementing counter with memcached key that does not exists, the value sets to initial counter value.

Expected behavior
Value setted to initial counter value plus increment step.

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.