Home Backend Development PHP Tutorial PHP Redis class operations

PHP Redis class operations

Jul 25, 2016 am 08:46 AM

  1. /************************************************ **********************************
  2. * InitPHP 2.0 Domestic PHP development framework Dao-Nosql-Redis
  3. *------------------------------------------------ -------------------------------
  4. * Copyright: CopyRight By initphp.com
  5. * You can use the source code freely, but During use, please retain the author information.尊重他人劳动成果就是尊重自己
  6. *-------------------------------------------------------------------------------
  7. * $Author:zhuli
  8. * $Dtime:2011-10-09
  9. ***********************************************************************************/
  10. class redisInit {
  11. private $redis; //redis对象
  12. /**
  13. * Initialize Redis
  14. * $config = array(
  15. * 'server' => '127.0.0.1' server
  16. * 'port' => '6379' port number
  17. * )
  18. * @param array $config
  19. */
  20. public function init($config = array()) {
  21. if ($config['server'] == '') $config['server'] = '127.0.0.1';
  22. if ($config['port'] == '') $config['port'] = '6379';
  23. $this->redis = new Redis();
  24. $this->redis->connect($config['server'], $config['port']);
  25. return $this->redis;
  26. }
  27. /**
  28. * Set value
  29. * @param string $key KEY name
  30. * @param string|array $value Get the data
  31. * @param int $timeOut time
  32. */
  33. public function set($key, $value, $timeOut = 0) {
  34. $value = json_encode($value, TRUE);
  35. $retRes = $this->redis->set($key, $value);
  36. if ($timeOut > 0) $this->redis->setTimeout($key, $timeOut);
  37. return $retRes;
  38. }
  39. /**
  40. * Get data through KEY
  41. * @param string $key KEY name
  42. */
  43. public function get($key) {
  44. $result = $this->redis->get($key);
  45. return json_decode($result, TRUE);
  46. }
  47. /**
  48. * Delete a piece of data
  49. * @param string $key KEY name
  50. */
  51. public function delete($key) {
  52. return $this->redis->delete($key);
  53. }
  54. /**
  55. * Clear data
  56. */
  57. public function flushAll() {
  58. return $this->redis->flushAll();
  59. }
  60. /**
  61. * Data is put into the queue
  62. * @param string $key KEY name
  63. * @param string|array $value Get the obtained data
  64. * @param bool $right Whether to start from the right
  65. */
  66. public function push($key, $value ,$right = true) {
  67. $value = json_encode($value);
  68. return $right ? $this->redis->rPush($key, $value) : $this->redis->lPush($key, $value);
  69. }
  70. /**
  71. * Dequeue data
  72. * @param string $key KEY name
  73. * @param bool $left Whether to start dequeuing data from the left
  74. */
  75. public function pop($key , $left = true) {
  76. $val = $left ? $this->redis->lPop($key) : $this->redis->rPop($key);
  77. return json_decode($val);
  78. }
  79. /**
  80. * Data auto-increment
  81. * @param string $key KEY name
  82. */
  83. public function increment($key) {
  84. return $this->redis->incr($key);
  85. }
  86. /**
  87. * Data decrement
  88. * @param string $key KEY name
  89. */
  90. public function decrement($key) {
  91. return $this->redis->decr($key);
  92. }
  93. /**
  94. * Whether the key exists, true is returned if it exists
  95. * @param string $key KEY name
  96. */
  97. public function exists($key) {
  98. return $this->redis->exists($key);
  99. }
  100. /**
  101. * Return the redis object
  102. * Redis has many operation methods, we only encapsulate some of them
  103. * With this object, you can directly call redis's own methods
  104. */
  105. public function redis() {
  106. return $this->redis;
  107. }
  108. }
复制代码

PHP, Redis
本主题由 小贝 于 2015-11-12 08:43 移动


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles