invincible-tech-systems/easeampredis 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

invincible-tech-systems/easeampredis

Composer 安装命令:

composer require invincible-tech-systems/easeampredis

包简介

A very simple and safe PHP library that provides methods to access redis cache in php applications. This wraps up the AmPHP related Redis Package to interact with Redis in-memory cache in an asynchronous & non-blocking way.

README 文档

README

A very simple and safe PHP library that provides methods to access Redis cache & Redis Pubsub (i.e., Redis is used as a message queue) in php applications. This wraps up the AmPHP related Redis Package to interact with Redis in-memory cache in an asynchronous & non-blocking way.

Advantages

  • Query Redis Cache in asynchronous & non-blocking way
  • Handle Channel Subscribe, UnSubscribe actions for single channel as well as a channel pattern, along with channel publish operation, when interacting with Redis Pubsub.

Getting started

With Composer, run

composer require invincible-tech-systems/easeampredis:^1.0.3

Note that the vendor folder and the vendor/autoload.php script are generated by Composer; they are not part of EaseAmpRedis.

To include the library,

<?php
require 'vendor/autoload.php';

use InvincibleTechSystems\EaseAmpRedis\EaseAmpRedis;

As Amphp/dns is among the dependencies of this library, to prevent recursive DNS Server resolution errors that may occur due reasons like open_basedir restrictions/ no access to /etc/resolv.conf file on the linux server etc..., do include the following lines in your code,

use \InvincibleTechSystems\EaseAmpRedis\CustomAmphpDnsConfigLoader;

$customAmphpDnsConfigValues = ["208.67.222.222:53", "208.67.220.220:53","8.8.8.8:53","[2001:4860:4860::8888]:53"];

$CustomAmphpDnsConfigLoader = new CustomAmphpDnsConfigLoader($customAmphpDnsConfigValues, 5000, 3);

\Amp\Dns\resolver(new \Amp\Dns\Rfc1035StubResolver(null, $CustomAmphpDnsConfigLoader));

Note: Do skip including the above, if incase similar custom DNS Config Loader is loaded from any of the other Amphp/dns dependent libraries like EaseAmyMysql (https://github.com/invincible-tech-systems/easeamp-mysql) or EaseAmpMysqlRedis (https://github.com/invincible-tech-systems/easeamp-mysql-redis) or EaseAmpMysqlHalite (https://github.com/invincible-tech-systems/easeampmysql-halite) in the application.

To Connect to Redis Server, without Password

$redisHost = "tcp://10.124.0.3:6379";
$redisConnection = new EaseAmpRedis($redisHost);

To Connect to Redis Server, with Password, wherein, the password that is going to be used in the below is the one that is defined using requirepass directive in the redis.conf file.

$redisHost = "tcp://localhost:6379?password=mypassword";
$redisConnection = new EaseAmpRedis($redisHost);

Note: In both with password & without password scenario based examples, port number 6379 is assumed to be used as that is the default port, as defined by Redis Software. The port number has to be changed, if Redis is hosted on a different port number.

Defining the Namespace Prefix

$namespacePrefix = "My-First-App";

Note: Do define the namespace, the way required, to uniquely identify the specific application and to segregate keys appropriately.

To Check if a Key Exists in Redis Cache

$existsCheckResult = $redisConnection->exists($namespacePrefix, 'foo');

The exists method expects a redis key namespace prefix along with key suffix as two parameters. 'foo' is the key suffix, as per this example. In this context, if the redis key is available, then the result will be returned as boolean(true), anything otherwise, the result will be boolean(false).

To Check the Type of the Key in Redis Cache

$typeCheckResult = $redisConnection->type($namespacePrefix, 'foo');

The type method expects a redis key namespace prefix along with key suffix as two parameters. 'foo' is the key suffix, as per this example. In this context, the result will the data type of the value of specific Redis Key. If the result is a valid one, it can be in the lines of string, list, set, zset, hash, anything otherwise, it will be 'None'.

To Set the Expiry Time to a Key in Redis Cache

$result = $redisConnection->expire($namespacePrefix, 'foo',3000);

The expire method expects three parameters as input, that constitutes a redis key namespace prefix, redis key suffix and the number of seconds for the key to expire (defined as milliseconds). 'foo' is the key suffix and 3000 is the number of milliseconds definition, as per this example. In this context, the result of the operation will be boolean(true) if successful and anything otherwise, the result will be boolean(false).

To Increment the Value of the Key in the Redis cache

$result = $redisConnection->incrBy($namespacePrefix, 'foo', 3);

The incrBy method can be used, that accepts three parameters as input like a redis key namespace prefix, redis key suffix and the number to do the increment operation. If in case the key do not exist, it is set to 0 before performing the operation.

To Decrement the Value of the Key in the Redis cache

$result = $redisConnection->decrBy($namespacePrefix, 'foo', 3);

The decrBy method can be used, that accepts three parameters as input like a redis key namespace prefix, redis key suffix and the number to do the decrement operation. If in case the key do not exist, it is set to 0 before performing the operation.

To Set Single or Multiple keys and Set their Values as Strings Passing in Data as Associative Array

$result = $redisConnection->stringSet(['f33'=>'jai','h33'=>'ram']);

The stringSet method sets the keys('f33','h33') values with specified strings('jai','ram'). The result will be true if operation is success otherwise the result will be false.

To Push the Values to the list of the Specified Redis Key, from head and in the form of array, in Redis cache

$result = $redisConnection->listPush('H', $namespacePrefix, 'list100', ['a','b']);

Note: The listPush method returns the length of the list after the push operation.

To Push the Values to the list of the Specified Redis key, from Tail and in the form of array, in Redis cache

$result = $redisConnection->listPush('T', $namespacePrefix, 'list100', ['c','d']);

Note: The listPush method returns the length of the list after the push operation.

To Remove the Values from the list of the Specified Redis key, from Head, in Redis cache

$result = $redisConnection->listPop('H', $namespacePrefix, 'list100');

Note: The listPop method returns the deleted element of the list after the pop operation.

To Remove the Values from the list of the Specified Redis key, from Tail, in Redis cache

$result = $redisConnection->listPop('T', $namespacePrefix, 'list100');

Note: The listPop method returns the deleted element of the list after the pop operation.

To Set the Specified Fields to their respective values in the Hash stored at Key, in Redis cache

$result = $redisConnection->mapSet($namespacePrefix, 'map100', ['yoo'=>'1','moo'=>'2']);

This command overwrites any specified fields already existing in the hash. If the key does not exist, a new key holding a hash is created. The result will be true if operation is success, anything else, the result will be false.

To Get the Value of the Redis key, from Redis cache

$result = $redisConnection->get($namespacePrefix, 'f33');

according to type of data of specific Redis key, get method returns the value using appropriate redis command internally

    if type of key is string, get method returns the value of the key as a string
                                      
    if type of key is list, get method returns the value of the key as a numeric index array
                                      
    if type of key is hash, get method returns the value of the key as an associative array

To Subscribe to a Channel, on Redis Pubsub

$result = $redisConnection->subscribe('hello');

A Channel can be subscribed to in the application using subscribe method. Since this activity is a blocking operation, it is advised to extend the EaseAmpRedis Class and then write code related to respective business logic inside the subscribe method in the extended class.

To pSubscribe to multiple channels, based on the Specified Pattern, on Redis Pubsub

$result = $redisConnection->pSubscribe('h?llo');

One or more Channels can be subscribed to in the application using pSubscribe method, based on specified pattern('h?llo'). Examples include: channel names like 'hello','htllo','hxllo' etc. Since this activity is a blocking operation, it is advised to extend the EaseAmpRedis Class and then write code related to respective business logic inside the pSubscribe method in the extended class.

To Publish a Message to a Channel, on Redis Pubsub

$result = $redisConnection->publish('koo','Hello');

The publish method publishes the message('Hello') to the channel('koo'). If the message is successfully published to the specified channel, then all those users (i.e., web socket clients) who had subscribed to that channel will receive the message.

To Unsubscribe a Specific Channel, on Redis Pubsub

$result = $redisConnection->unSubscribe('koo');

The unSubscribe method unsubscribes the user from the specified channel('koo'). If the result is true, the user will not receive any messages that are published to that channel thereafter.

To Unsubscribe from multiple channels, based on the Specified Pattern, on Redis Pubsub

$result = $redisConnection->pUnSubscribe('h?llo');

The pUnSubscribe method unsubscribes the user from all channels, that are based on specified pattern('h?llo'). Examples include: channel names like 'hello','htllo','hxllo' etc. If the result is true, the user will not receive any messages that are published to all the channels matching that specified pattern.

Contributors

Krishnaveni (krishnaveni.nimmala@invincibletechsystems.com)

License

This software is distributed under the MIT license. Please read LICENSE for information on the software availability and distribution.

invincible-tech-systems/easeampredis 适用场景与选型建议

invincible-tech-systems/easeampredis 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 45 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「redis」 「php」 「cache」 「asynchronous」 「non-blocking」 「amphp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 invincible-tech-systems/easeampredis 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 invincible-tech-systems/easeampredis 我们能提供哪些服务?
定制开发 / 二次开发

基于 invincible-tech-systems/easeampredis 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 45
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-25