Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use php code sniffer #30

Merged
merged 19 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
3b6cc88
feat: use php code sniffer
rishtigupta Mar 12, 2024
5d841f1
feat: update MomentoBackendFactory.php code based on phpcs results
rishtigupta Mar 12, 2024
cd97e1e
fix: more linting in MomentoCcaheBackendFactory.php
rishtigupta Mar 12, 2024
cb69080
fix: more linting in MomentoCcaheBackendFactory.php
rishtigupta Mar 12, 2024
80ac202
fix: more linting in MomentoCcaheBackendFactory.php
rishtigupta Mar 12, 2024
4d9a738
fix: more linting in MomentoCcaheBackendFactory.php
rishtigupta Mar 12, 2024
161ef7e
fix: remove phpcs warnings for README
rishtigupta Mar 12, 2024
8ddbf8f
fix: remove phpcs warnings for README
rishtigupta Mar 12, 2024
96a4601
fix: update MomentoCacheBackend.php based on phpcs
rishtigupta Mar 12, 2024
910a657
fix: update MomentoCacheBackend.php based on phpcs
rishtigupta Mar 12, 2024
e1e6a38
fix: update MomentoCacheBackend.php based on phpcs
rishtigupta Mar 12, 2024
9a4c1c1
fix: update MomentoCacheBackend.php based on phpcs
rishtigupta Mar 12, 2024
a3620e4
fix: update MomentoCacheBackendTest.php based on phpcs
rishtigupta Mar 12, 2024
70f2e44
fix: update MomentoCacheBackendTest.php based on phpcs
rishtigupta Mar 12, 2024
3a9f19e
fix: update MomentoCacheBackendTest.php based on phpcs
rishtigupta Mar 12, 2024
3c8002e
fix: uncomment the PHPCS commands
rishtigupta Mar 13, 2024
6cd15a6
fix: update MomentoClientFactory based on phpcs results
rishtigupta Mar 13, 2024
5872051
fix: update MomentoClientFactory based on phpcs results
rishtigupta Mar 13, 2024
444ace5
fix: resolve phpcs linting issue
rishtigupta Mar 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ jobs:
- name: Run PHPCS
working-directory: ${{ env.DRUPAL_ROOT }}
run: |
# vendor/bin/phpcs $MODULE_FOLDER --standard=Drupal \
# --extensions=php,module,inc,install,test,info
vendor/bin/phpcs $MODULE_FOLDER --standard=Drupal \
--extensions=php,module,inc,install,test,info

- name: Start Drush webserver and chromedriver
working-directory: ${{ env.DRUPAL_ROOT }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/on-push-to-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ jobs:
- name: Run PHPCS
working-directory: ${{ env.DRUPAL_ROOT }}
run: |
# vendor/bin/phpcs $MODULE_FOLDER --standard=Drupal \
# --extensions=php,module,inc,install,test,info
vendor/bin/phpcs $MODULE_FOLDER --standard=Drupal \
--extensions=php,module,inc,install,test,info

- name: Start Drush webserver and chromedriver
working-directory: ${{ env.DRUPAL_ROOT }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
.idea/
.DS_Store
47 changes: 36 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,29 @@ A Momento API Token is required. You can generate one using the [Momento Console

The Momento Cache module uses [Momento's PHP SDK](https://docs.momentohq.com/sdks#php-sdk) internally. While installing the Drupal module will automatically install the SDK for you. Separately, you will need to install and enable the following extensions:
1. [The PHP gRPC extension](https://github.com/grpc/grpc/blob/master/src/php/README.md) for the SDK to function.
2. The C Protobuf extension for PHP. You can install it using `pecl install protobuf` and then add `extension=protobuf.so` to your `php.ini` file.
2. [The C Protobuf extension](https://developers.google.com/google-ads/api/docs/client-libs/php/protobuf#c_implementation) for PHP.


A Momento cache is required to handle Drupal's caching requests. You can create a cache in the [Momento Console](https://console.gomomento.com/).

### Drupal and Momento Rate Limiting

Momento's free tier limits accounts' transactions per second (TPS) and throughput (KiBps), and requests that exceed those limits are throttled. While the default limits are fine for exploring and developing with the Momento integration, Drupal's usage of the cache backend can be significantly higher than the default throttling limits under load. Prior to using the integration in a high-traffic or production environment, please reach out to `[email protected]` to raise your account limits. You can check the Drupal dblog and/or the logfile you configure in the settings (instructions below) for rate limiting error messages.
Momento's free tier limits accounts' transactions per second (TPS) and
throughput (KiBps), and requests that exceed those limits are throttled.
While the default limits are fine for exploring and developing with
the Momento integration, Drupal's usage of the cache backend can be
significantly higher than the default throttling limits under load.
Prior to using the integration in a high-traffic or production environment,
please reach out to `[email protected]` to raise your account limits.
You can check the Drupal dblog and/or the logfile you configure in the
settings (instructions below) for rate limiting error messages.

## Installation and Configuration

Add the module with `composer require 'momentohq/drupal-cache:0.5.2'`.

Enable the `momento_cache` module in your Drupal administrator interface or using `drush en momento_cache`.
Enable the `momento_cache` module in your Drupal administrator interface
or using `drush en momento_cache`.

Add the following to your `settings.php` file:

Expand All @@ -30,11 +39,27 @@ $settings['momento_cache']['cache_name'] = '<YOUR_CACHE_NAME>';
$settings['momento_cache']['logfile'] = '<YOUR_LOG_FILE_PATH>';
```

Replace `<YOUR_MOMENTO_TOKEN>` with the token you generated in the console. You may also use an environment variable named `MOMENTO_API_TOKEN` to pass your API token to the Momento cache backend. If both are supplied, the settings file takes precedence.

Replace `<YOUR_CACHE_NAME>` with the name of your existing Momento cache. You may also use an environment variable named `MOMENTO_CACHE_NAME` to pass this value. If both are supplied, the settings file takes precedence. **You must create the cache before using the module.** If the cache doesn't exist, errors are written to the Drupal dblog as well as the logfile configured in your settings, if you have specified one.

Replace `<YOUR_LOG_FILE_PATH>` with the path of a file writable by your Drupal server or with `null` if you want to suppress logging. This logfile is used for logging and timing requests as they are handled by the module. Please be aware that this file will grow quickly over time, so if you choose to enable it long-term, you should probably use `logrotate` or some similar utility to keep it from growing out of control.

The example above uses Momento for all Drupal caches. If you prefer to use Momento for specific cache bins, you may assign them individually as well: `$settings['cache']['bins']['render'] = 'cache.backend.momento_cache'`

Replace `<YOUR_MOMENTO_TOKEN>` with the token you generated in the console.
You may also use an environment variable named `MOMENTO_API_TOKEN` to pass
your API token to the Momento cache backend. If both are supplied,
the settings file takes precedence.

Replace `<YOUR_CACHE_NAME>` with the name of your existing Momento cache.
You may also use an environment variable named `MOMENTO_CACHE_NAME`
to pass this value. If both are supplied, the settings file takes precedence.
**You must create the cache before using the module.**
If the cache doesn't exist, errors are written to the Drupal dblog
as well as the logfile configured in your settings, if you have specified one.

Replace `<YOUR_LOG_FILE_PATH>` with the path of a file writable by your
Drupal server or with `null` if you want to suppress logging.
This logfile is used for logging and timing requests as they are handled
by the module. Please be aware that this file will grow quickly over time,
so if you choose to enable it long-term, you should probably
use `logrotate` or some similar utility to keep it from
growing out of control.

The example above uses Momento for all Drupal caches. If you prefer
to use Momento for specific cache bins, you may assign them
individually as well:
`$settings['cache']['bins']['render'] = 'cache.backend.momento_cache'`
123 changes: 84 additions & 39 deletions src/Client/MomentoClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,96 @@
use Momento\Cache\CacheClient;
use Momento\Config\Configurations\Laptop;

/**
* Momento client factory.
*/
class MomentoClientFactory {

private $authProvider;
private $client;
private $clientTimeoutMsec;
private $forceNewChannel;
private $numGrpcChannels;
private $logFile;
/**
* The authentication provider.
*
* @var \Momento\Auth\StringMomentoTokenProvider
*/
private $authProvider;

public function __construct() {
$settings = Settings::get('momento_cache', []);
$authToken = array_key_exists('api_token', $settings) ?
/**
* The cache client.
*
* @var \Momento\Cache\CacheClient
*/
private $client;

/**
* The client timeout in milliseconds.
*
* @var int
*/
private $clientTimeoutMsec;

/**
* Whether to force a new channel.
*
* @var bool
*/
private $forceNewChannel;

/**
* The number of gRPC channels.
*
* @var int
*/
private $numGrpcChannels;

/**
* The log file.
*
* @var string
*/
private $logFile;

/**
* MomentoClientFactory constructor.
*/
public function __construct() {
$settings = Settings::get('momento_cache', []);
$authToken = array_key_exists('api_token', $settings) ?
$settings['api_token'] : getenv("MOMENTO_API_TOKEN");
$this->authProvider = new StringMomentoTokenProvider($authToken);
$this->forceNewChannel = array_key_exists('force_new_channel', $settings) ?
$settings['force_new_channel'] : true;
$this->logFile =
array_key_exists('logfile', $settings) ? $settings['logfile'] : null;
$this->numGrpcChannels =
$this->authProvider = new StringMomentoTokenProvider($authToken);
$this->forceNewChannel = array_key_exists('force_new_channel', $settings) ?
$settings['force_new_channel'] : TRUE;
$this->logFile =
array_key_exists('logfile', $settings) ? $settings['logfile'] : NULL;
$this->numGrpcChannels =
array_key_exists('num_grpc_channels', $settings) ? $settings['num_grpc_channels'] : 1;
$this->clientTimeoutMsec =
$this->clientTimeoutMsec =
array_key_exists('client_timeout_msec', $settings) ? $settings['client_timeout_msec'] : 15000;
}
}

public function get() {
if ($this->client) {
return $this->client;
}
$start = hrtime(true);
$config = Laptop::latest();
$config = $config->withTransportStrategy(
$config->getTransportStrategy()->withGrpcConfig(
$config
->getTransportStrategy()
->getGrpcConfig()
->withForceNewChannel($this->forceNewChannel)
->withNumGrpcChannels($this->numGrpcChannels)
)
)->withClientTimeout($this->clientTimeoutMsec);
$this->client = new CacheClient($config, $this->authProvider, 30);
if ($this->logFile) {
$totalTimeMs = (hrtime(true) - $start) / 1e6;
$mt = microtime(true);
@error_log("[$mt] Instantiated cache client [$totalTimeMs ms]\n", 3, $this->logFile);
}
return $this->client;
/**
* Get the momento client.
*/
public function get() {
if ($this->client) {
return $this->client;
}
$start = hrtime(TRUE);
$config = Laptop::latest();
$config = $config->withTransportStrategy(
$config->getTransportStrategy()->withGrpcConfig(
$config
->getTransportStrategy()
->getGrpcConfig()
->withForceNewChannel($this->forceNewChannel)
->withNumGrpcChannels($this->numGrpcChannels)
)
)->withClientTimeout($this->clientTimeoutMsec);
$this->client = new CacheClient($config, $this->authProvider, 30);
if ($this->logFile) {
$totalTimeMs = (hrtime(TRUE) - $start) / 1e6;
$mt = microtime(TRUE);
@error_log("[$mt] Instantiated cache client [$totalTimeMs ms]\n", 3, $this->logFile);
}
return $this->client;
}

}
Loading
Loading