Home  >  Article  >  Backend Development  >  self-increasing counter

self-increasing counter

WBOY
WBOYOriginal
2016-07-25 08:43:551065browse
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


Statement:
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
Previous article:PHP submits data via PostNext article:PHP submits data via Post