Skip to content

Commit b62f967

Browse files
committed
First version.
1 parent 213af5d commit b62f967

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
# mcounter
2-
Simple memcache counter.
2+
Dead simple memcached counter.
3+
4+
NB! There is only abstract classes, because it's meant to be redefined.
5+
6+
```php
7+
class MyCounter extends \Dorantor\AbstractCounter
8+
{
9+
/**
10+
* Method for building cache key based on $item value
11+
*
12+
* @return string
13+
*/
14+
protected function getKey()
15+
{
16+
return 'myCounter' . (int) $this->item->id;
17+
}
18+
}
19+
```
20+
21+
And usage:
22+
```php
23+
// $client creation is up to you.
24+
// Most probably you already created one earlier, so just reuse it here.
25+
$counter = new MyCounter($user, $client);
26+
if ($counter->value() < 100) {
27+
$counter->inc();
28+
}
29+
```

composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "dorantor/mcounter",
3+
"description": "Very simple lib for memcached based counters.",
4+
"keywords": ["memcached", "counter", "cipher"],
5+
"homepage": "https://github.com/dorantor/mcounter/",
6+
"type": "library",
7+
"license": "MIT",
8+
"require": {
9+
"php": "^5.6|^7.0",
10+
"ext-memcached": "*"
11+
},
12+
"autoload": {
13+
"psr-4" : {
14+
"Dorantor\\" : "src"
15+
}
16+
}
17+
}

src/AbstractCounter.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the dorantor/mcounter package.
5+
*/
6+
7+
namespace Dorantor;
8+
9+
abstract class AbstractCounter
10+
{
11+
/**
12+
* @var \Memcached
13+
*/
14+
private $client;
15+
16+
/**
17+
* @var \Dorantor\CountableInterface
18+
*/
19+
protected $item;
20+
21+
/**
22+
* AbstractCounter constructor.
23+
*
24+
* @param mixed $item
25+
* @param \Memcached $client
26+
*/
27+
public function __construct($item, \Memcached $client)
28+
{
29+
$this->setClient($client);
30+
$this->item = $item;
31+
}
32+
33+
/**
34+
* Set memcached client object
35+
*
36+
* @param \Memcached $client
37+
*/
38+
public function setClient(\Memcached $client)
39+
{
40+
$this->client = $client;
41+
}
42+
43+
/**
44+
* Get current counter value
45+
*
46+
* @return int
47+
*/
48+
public function value()
49+
{
50+
return $this->client->get($this->getKey());
51+
}
52+
53+
/**
54+
* Increase counter
55+
*
56+
* @param int $step
57+
*
58+
* @return int value after increase
59+
*/
60+
public function inc($step = 1)
61+
{
62+
return $this->client->increment($this->getKey(), $step, $this->getInitialalue());
63+
}
64+
65+
/**
66+
* Initial counter value
67+
* Could be redefined in descendants
68+
*
69+
* @return int
70+
*/
71+
protected function getInitialValue()
72+
{
73+
return 0;
74+
}
75+
76+
/**
77+
* Method for building cache key based on $item value
78+
*
79+
* @return string
80+
*/
81+
abstract protected function getKey();
82+
}

src/CountableInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Dorantor;
4+
5+
interface CountableInterface
6+
{}

0 commit comments

Comments
 (0)