Home > Backend Development > PHP Tutorial > self-increasing counter

self-increasing counter

WBOY
Release: 2016-07-25 08:43:55
Original
1116 people have browsed it
Self-increasing counter based on redis, generating self-increasing numbers
  1. /**
  2. * Generate self-increasing number
  3. *
  4. * @param string $key cache key
  5. * @param int $step self-increasing step size
  6. * @param int $expires cache expiration time, unit seconds
  7. *
  8. * @return int $num ;
  9. * @author leeyi
  10. */
  11. function incr_num($key='ddg', $step=1, $expires=0) {
  12. // The link redis is encapsulated in OrgRedis()
  13. $redis = new OrgRedis();
  14. $cache_key = 'incrnum:'.$key;
  15. $num = $redis->handler->incrBy($cache_key, (int)$step);
  16. $millisecond = $expires> 0 ? $expires*1000 : (get_time_235959()*1000+999);
  17. $redis->handler->pexpireAt($cache_key, $millisecond); // Set expiration time
  18. return $num;
  19. }
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template