this article illustrates the definition and usage of the PHP based redis counter class. I want to share with you the following details:

Redis is an open source, log and Key-Value database that is written and supported by ANSI C language, supported by network, and can be persisted based on memory. It also provides API for many languages.

here uses its incr (self increasing) ,

1.Redis counter class code and demo

RedisCounter.class.php

 < PHP; / * * * PHP? Based on Redis * Date: * Author: 2017-10-28 counter class fdipzone * 1 * Version: * Descripton: * PHP based on the realization of Redis increment count, incr method is mainly used redis, concurrent execution ensure the count increment only. * * Func: * public incr is incremented count and get increment after public get to obtain the current count value * * public reset * private connect reset count create redis / class RedisCounter{/ / class start connection private $_config; private $_redis; / * * * * @ param Array $config initialization redis connection set / public ($config) function __construct {$this-> _config = $config; $this-> _redis = $this-> (connect);} / * * * incremented count and get the value increment of the @param * String $key save count key value * @param Int $incr since the increase in the number, the default is 1 * @return Int * public function incr ($key, $incr=1) {return intval ($this-> _redis-> incr ($key, $incr));} / * * * to get the current meter The number of String * @param $key save the count of the health value of * @return Int * public function get ($key) {return ($this-> intval _redis-> get ($key));} / * * * * @param String $key to save the reset count count of the health value of Int / public function * @return reset ($key) {return $this-> _redis-> delete; ($key);} / * * * * * Link @return to create a redis connection private function (connect) {try{$redis = new (Redis); $redis-> connect ($this-> _config['host'], $this-> _config['port'], _config['timeout'], $this-> & this-> _config['reserved']; $this-> _config['retry_interval']; if (empty) ($this-> _config['auth']) auth ($this->) {$redis-> _config['auth']; $redis-> sel;}); ECT ($this-> _config['index']);}catch (RedisException $e) {throw new Exception ($e->); getMessage (return); false return $redis;};}} / / class end? > 

demo.php

 < PHP Require'RedisCounter.class.php'; / / redis? Array ($config = connection settings'host' =>'port'=>'localhost','index'6379, => 0,'auth'; =>'timeout'=> 1','reserved', NULL, =>'retry_interval' => 100); / / create a RedisCounter object $oRedisCounter = new ($config RedisCounter); / / define save the count of the health value of $key ='mycounter'; / / is incremented get the current count, count reset count, echo $oRedisCounter-> get.PHP_EOL ($key); / / echo 0 $oRedisCounter-> incr ($key.PHP_EOL); / / 1 echo $oRedisCounter-> incr ($key, 10).PHP_EOL / / 11 echo; $oRedisCounter-> reset ($key.PHP_EOL); / / 1 echo ($key) $oRedisCounter-> get.PHP_EOL; > 

; / / the 0?

output:

 011110 

test code as follows:

 < PHP Require?'RedisCounter.class.php'; / / redis connection set $config = array ('host' =>'localhost''port' => 6379,'index'0,'auth', => =>'timeout'=> 1','reserved', => NULL,'retry_interval' => 100); / / create a RedisCounter object; $oRedisCounter = new RedisCounter ($config); / / define save count The health value of $key ='mytestcounter'; / / incremented counter and returns count increment after the record into the temporary file file_put_contents ('/tmp/mytest_result.log', $oRedisCounter-> incr ($key).PHP_EOL, FILE_APPEND); > 

?

tests are executed concurrently, we use the ab tool for testing set

 ab -c -n 150 http://localhost/test.php 

15

 ab -c execution results: 15 -n 150 http://localhost/test.php This is ApacheBench, Version 2.3 < $Revision: 1554214 $> 1996 Copyright Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking home.rabbit.km.com (be patient)..done Server Software: nginx/1.6.3 Server Hostname:... Localhost Server Port: 80 Document Path Document Length: bytes Concurrency: /test.php 0 Level: 15 Time taken for tests: 0.173 seconds Complete requests: 150 Failed requests: 0 Total transferred: 24150 bytes HTML transferred: 0 bytes Requests per second: 864.86 [#/sec] (mean) Time per request: 17.344 [ms] (mean) Time per request: 1.156 [ms] (mean, across all concurrent requests Transfer rate: [Kbytes/sec] received Connection) 135.98 Times (MS) min mean[+/-sd] median Max Connect: 0.201 Processing: 3163.21623 Waiting: 3163.21623 Total: 4163.11723 Percentage of the requests served within a certain time (MS) 50% 1766% 1875% 1880% 1990% 2095% 2198% 2299% 22100% 23 (longest request) 

WC -l to check whether the total count count /tmp/mytest_result.log only

 generated 150 /tmp/mytest_result.log generated sort -u /tmp/mytest_result.log | only count WC -l 150 

can see that the generated count is also unique in the case of concurrent calls.

more PHP on the relevant content of interested readers can see the special station: "php+redis database programming skills summary ", "php object-oriented programming tutorial" , PHP ", the basic grammar tutorial "PHP array (Array) technique," php string (string) " php+mysql database operation tutorial " and "php " common database operation skills summary

hope this the PHP program designed to help all of you.

This paper fixed link:http://www.script-home.com/php-definition-and-usage-examples-based-on-the-redis-counter-class.html | Script Home | +Copy Link

Article reprint please specify:PHP definition and usage examples based on the redis counter class | Script Home

You may also be interested in these articles!