Skip to content

2.0 Refactor #5

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

Merged
merged 57 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
7c7b6d3
Merge tag 'v1.0.0' into develop
jasonmccallister Nov 3, 2020
2a76b18
Use sessionhandler
timkelty Mar 12, 2022
ef10963
Multi-tenant
timkelty Mar 13, 2022
2980e48
Connection class
timkelty Mar 30, 2022
c11bf77
Connection/session
timkelty Apr 1, 2022
fff1fa5
Cache
timkelty Apr 1, 2022
52048e2
stub
timkelty Apr 1, 2022
0f976bc
hint
timkelty Apr 1, 2022
529277d
compound keys
timkelty Apr 1, 2022
a739921
Scalar hint
timkelty Apr 1, 2022
838f12c
Format key
timkelty Apr 1, 2022
c263ae2
Fix delete
timkelty Apr 1, 2022
87ec137
Queue
timkelty Apr 5, 2022
0fbb12f
pk default
timkelty Apr 6, 2022
31717fc
table test
timkelty Apr 6, 2022
4403ae6
Tests
timkelty Apr 6, 2022
cdaf080
php 8
timkelty Apr 6, 2022
9362d69
Fix test
timkelty Apr 6, 2022
605d6b4
Add types
timkelty Apr 6, 2022
1c9daa1
add Scan delte
timkelty Apr 6, 2022
98cdbc6
Fix session tests
timkelty Apr 6, 2022
5e25f46
Configure env for tests
timkelty Apr 8, 2022
ecb386e
Improve methods
timkelty Apr 9, 2022
373ed60
Refactor flush
timkelty Apr 10, 2022
ec16f25
Tests
timkelty Apr 10, 2022
df2b16e
Config queue
timkelty Apr 10, 2022
d9c6a80
Test
timkelty Apr 10, 2022
2c69b00
Queue test
timkelty Apr 10, 2022
e61fc5b
Simple queue
timkelty Apr 11, 2022
4904fbd
ttl for sessions from ini
timkelty Apr 12, 2022
c90b19d
timeout set on session
timkelty Apr 12, 2022
cfb72da
Comments
timkelty Apr 12, 2022
4a2e07f
GC, timeout for sessions
timkelty Apr 12, 2022
aa3ad30
Default table names
timkelty Apr 12, 2022
2d4d233
Catch session/cache
timkelty Apr 12, 2022
30c0f5f
Wrap in try/catch
timkelty Apr 12, 2022
5180324
Add types, phpdoc
timkelty Apr 12, 2022
905e2cb
Fix ttl
timkelty Apr 12, 2022
62f8b5f
Add throws
timkelty Apr 13, 2022
67449d3
Better key
timkelty Apr 13, 2022
16e2c15
Put item tests
timkelty Apr 13, 2022
dc1f9e8
Have putItem actually putItem
timkelty Apr 13, 2022
56908c1
Uppercase special attributes, require tableName
timkelty Apr 14, 2022
2dc8b1d
Whoopsie
timkelty Apr 14, 2022
aaad38e
ECS changes
timkelty Apr 18, 2022
4264ecb
PHPStan + ECS
timkelty Apr 18, 2022
a2c1c53
CI
timkelty Apr 18, 2022
4f460d7
Fix ci
timkelty Apr 18, 2022
86dd5af
🇺🇦
timkelty Apr 18, 2022
67181eb
bypass bower-asset
timkelty Apr 19, 2022
737c659
Update namespace
timkelty Apr 21, 2022
1a4a10a
Readme
timkelty Apr 21, 2022
eaa9839
Merge pull request #6 from pixelandtonic/craftcms-release
jasonmccallister Apr 21, 2022
86112f7
Changelog, linting
timkelty Apr 22, 2022
fc2dfb4
npm
timkelty Apr 22, 2022
48744c9
Prettier
timkelty Apr 22, 2022
dc44634
Husky
timkelty Apr 22, 2022
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
57 changes: 57 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: ci
on:
workflow_dispatch:
pull_request:
jobs:
ecs:
name: ECS
uses: craftcms/.github/.github/workflows/ecs.yml@v1
with:
php_version: '8.0'
phpstan:
name: PHPStan
uses: craftcms/.github/.github/workflows/phpstan.yml@v1
with:
php_version: '8.0'
phpunit:
runs-on: ubuntu-latest
name: PHPUnit
steps:
- uses: actions/checkout@v1

- name: Validate composer.json
run: composer validate

- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-composer-${{ hashFiles('composer.json') }}

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Setup DynamoDB
run: docker-compose up -d dynamodb

- name: Create DynamoDB tables
run: make tables
env:
AWS_REGION: local
AWS_ACCESS_KEY_ID: local
AWS_SECRET_ACCESS_KEY: local

- name: Run test suite
run: composer run-script test

notify-slack:
name: Notify Slack
needs: [ecs, phpstan, phpunit]
if: ${{ always() }}
uses: craftcms/.github/.github/workflows/notify-slack.yml@v1
with:
success: ${{ needs.ecs.result == 'success' && needs.phpstan.result == 'success' && needs.phpunit.result == 'success' }}
failure_text_prefix: <!subteam^SGFL9NKNZ>
secrets:
token: ${{ secrets.GITHUB_TOKEN }}
slack_webhook_url: ${{ secrets.SLACK_CRAFT_WEBHOOK_URL }}
36 changes: 0 additions & 36 deletions .github/workflows/run-tests.yml

This file was deleted.

1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
8 changes: 8 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# Add to path for GUIs (Tower, etc.)
PATH="/usr/local/bin:$PATH"
[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh

npx lint-staged
7 changes: 7 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"**/*.php": [
"./vendor/bin/ecs check --ansi --fix",
"./vendor/bin/phpstan analyse"
],
"*": "prettier --ignore-unknown --write"
}
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
vendor/*
build/*
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"bracketSpacing": false,
"vueIndentScriptAndStyle": true
}
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# Release Notes for Yii2 DynamoDB

## 2.0.0 - 2022-04-22

## Changed

- Composer packages has moved from `pixelandtonic/yii2-dynamodb` to `craftcms/dynamodb`.
- Namespaces have changed from `pixelandtonic\dynamodb\drivers\*` to `crafcms\dynamodb\*.`
- All components now share a `dynamoDb` property, which is a `craftcms\dynamodb\DynamoDbConnection` instance or configuration.
- Replaced `pixelandtonic\dynamodb\WithDynamoDbClient` with `craftcms\dynamodb\DynamoDbConnection`. Several properties have changed.
- Replaced `pixelandtonic\dynamodb\WithDynamoDbClient::keyPrefix` with `craftcms\dynamodb\DynamoDbConnection::formatKey`.
- A key prefix is no longer added by default to any components. You may add this to any using `craftcms\dynamodb\DynamoDbConnection::formatKey`.
- The default partition key is now `PK`, not `id`.

## Added

- Added `craftcms\dynamodb\DynamoDbConnection::partitionKeyAttribute` and `craftcms\dynamodb\DynamoDbConnection::sortKeyAttribute` to allow for compound keys.
- Added `craftcms\dynamodb\DynamoDbConnection::consistentRead`
- Added `craftcms\dynamodb\DynamoDbConnection::batchConfig`
- Added [TTL support](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html) via `craftcms\dynamodb\DynamoDbConnection::ttl` and `craftcms\dynamodb\DynamoDbConnection::ttlAttribute`.
- Added automatic TTL and garbage collection to the session component.

## Fixed

- Fixed an issue where an `AWS_REGION` would not automatically get used if present.
- Fixed an issue where the session component would not register itself as a session handler.

## 1.0.0 - 2020-11-03

## Added
Expand Down
45 changes: 31 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,36 @@ ENDPOINT_URL ?= http://localhost:8000
AWS_ACCESS_KEY_ID = local
AWS_SECRET_ACCESS_KEY = local

tables:
configure:
aws configure set aws_access_key_id ${AWS_ACCESS_KEY_ID}
aws configure set aws_secret_access_key ${AWS_SECRET_ACCESS_KEY}
cache:
aws --endpoint-url=${ENDPOINT_URL} --region=${REGION} \
dynamodb create-table --table-name=${CACHE_TABLE_NAME} \
--attribute-definitions=AttributeName=id,AttributeType=S \
--key-schema=AttributeName=id,KeyType=HASH \
--billing-mode=PAY_PER_REQUEST && \
dynamodb create-table --table-name ${CACHE_TABLE_NAME} \
--attribute-definitions \
AttributeName=PK,AttributeType=S \
AttributeName=SK,AttributeType=S \
--key-schema \
AttributeName=PK,KeyType=HASH \
AttributeName=SK,KeyType=RANGE \
--provisioned-throughput \
ReadCapacityUnits=5,WriteCapacityUnits=5
sessions:
aws --endpoint-url=${ENDPOINT_URL} --region=${REGION} \
dynamodb create-table --table-name=${SESSION_TABLE_NAME} \
--attribute-definitions=AttributeName=id,AttributeType=S \
--key-schema=AttributeName=id,KeyType=HASH \
--billing-mode=PAY_PER_REQUEST && \
aws --endpoint-url=${ENDPOINT_URL} --region=${REGION} \
dynamodb create-table --table-name=${QUEUE_TABLE_NAME} \
--attribute-definitions=AttributeName=id,AttributeType=S \
--key-schema=AttributeName=id,KeyType=HASH \
--billing-mode=PAY_PER_REQUEST
dynamodb create-table --table-name=${SESSION_TABLE_NAME} \
--attribute-definitions \
AttributeName=id,AttributeType=S \
--key-schema \
AttributeName=id,KeyType=HASH \
--provisioned-throughput \
ReadCapacityUnits=5,WriteCapacityUnits=5
queue:
aws --endpoint-url=${ENDPOINT_URL} --region=${REGION} \
dynamodb create-table --table-name=${QUEUE_TABLE_NAME} \
--attribute-definitions \
AttributeName=PK,AttributeType=S \
--key-schema \
AttributeName=PK,KeyType=HASH \
--provisioned-throughput \
ReadCapacityUnits=5,WriteCapacityUnits=5
tables: configure cache sessions queue
80 changes: 49 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Yii2 DynamoDB Cache, Session, and Queue Driver Implementation
# DynamoDB Cache, Session, and Queue for Yii 2

[![Latest Version on Packagist](https://img.shields.io/packagist/v/pixelandtonic/yii2-dynamodb.svg?style=flat-square)](https://packagist.org/packages/pixelandtonic/yii2-dynamodb)
[![Total Downloads](https://img.shields.io/packagist/dt/pixelandtonic/yii2-dynamodb.svg?style=flat-square)](https://packagist.org/packages/pixelandtonic/yii2-dynamodb)
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/pixelandtonic/yii2-dynamodb/run-tests?label=tests)](https://github.com/pixelandtonic/yii2-dynamodb/actions?query=workflow%3Arun-tests+branch%3Amaster)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/craftcms/yii2-dynamodb.svg?style=flat-square)](https://packagist.org/packages/craftcms/yii2-dynamodb)
[![Total Downloads](https://img.shields.io/packagist/dt/craftcms/yii2-dynamodb.svg?style=flat-square)](https://packagist.org/packages/craftcms/yii2-dynamodb)
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/craftcms/yii2-dynamodb/run-tests?label=tests)](https://github.com/pixelandtonic/yii2-dynamodb/actions?query=workflow%3Aci+branch%3Amaster)

Easily use DynamoDB as a [cache](https://www.yiiframework.com/doc/guide/2.0/en/caching-overview), [session](https://www.yiiframework.com/doc/guide/2.0/en/runtime-sessions-cookies), or [queue](https://github.com/yiisoft/yii2-queue) using this library in your Yii2 or Craft CMS projects.

Expand All @@ -13,12 +13,12 @@ Easily use DynamoDB as a [cache](https://www.yiiframework.com/doc/guide/2.0/en/c
You can install the package via composer:

```bash
composer require pixelandtonic/yii2-dynamodb
composer require craftcms/yii2-dynamodb
```

## Usage

This package provides three drivers for DynamoDB; caching, sessions, and queuing.
This package provides three Yii components for DynamoDB: cache, session, and queue.

### Cache Component

Expand All @@ -40,7 +40,7 @@ aws dynamodb create-table --table-name=my-app-cache-table \
In your `app.php`, configure the `cache` component to use the driver.

```php
use \pixelandtonic\dynamodb\drivers\DynamoDbCache;
use craftcms\dynamodb\DynamoDbCache;

return [
'bootstrap' => [
Expand All @@ -49,13 +49,19 @@ return [
'components' => [
'cache' => [
'class' => DynamoDbCache::class,
'table' => 'my-app-cache-table',
'tableIdAttribute' => 'id', // optional: defaults to id
'tableDataAttribute' => 'data', // optional: defaults to data
'endpoint' => 'http://localhost:8000', // optional: used for local or when using DAX
'key' => '<key>', // optional: defaults to AWS_ACCESS_KEY_ID env var
'secret' => '<secret>', // optional: defaults to AWS_SECRET_ACCESS_KEY env var
'region' => '<region>', // optional: defaults to AWS_REGION env var
'dataAttribute' => 'data', // optional: defaults to data
'dynamoDb' => [
'table' => 'my-app-cache-table',
'partitionKeyAttribute' => 'id', // optional: defaults to 'PK'
'endpoint' => 'http://localhost:8000', // optional: used for local or when using DAX
'region' => '<region>', // optional: defaults to AWS_REGION env var
'ttl' => 60*60*24, // optional: number of seconds until items are considered expired
'ttlAttribute' => 'expires' // optional: defaults to 'TTL'
'credentials' => [
'key' => '<key>', // optional: defaults to AWS_ACCESS_KEY_ID env var
'secret' => '<secret>', // optional: defaults to AWS_SECRET_ACCESS_KEY env var
],
],
],
],
];
Expand All @@ -81,7 +87,7 @@ aws dynamodb create-table --table-name=my-app-session-table \
In your `app.php`, configure the `session` component to use the driver.

```php
use \pixelandtonic\dynamodb\drivers\DynamoDbSession;
use craftcms\dynamodb\DynamoDbSession;

return [
'bootstrap' => [
Expand All @@ -90,13 +96,19 @@ return [
'components' => [
'session' => [
'class' => DynamoDbSession::class,
'table' => 'my-app-session-table',
'tableIdAttribute' => 'id', // optional: defaults to id
'tableDataAttribute' => 'data', // optional: defaults to data
'endpoint' => 'http://localhost:8000', // optional: used for local or when using DAX
'key' => '<key>', // optional: defaults to AWS_ACCESS_KEY_ID env var
'secret' => '<secret>', // optional: defaults to AWS_SECRET_ACCESS_KEY env var
'region' => '<region>', // optional: defaults to AWS_REGION env var
'dataAttribute' => 'data', // optional: defaults to data
'dynamoDb' => [
'table' => 'my-app-session-table',
'partitionKeyAttribute' => 'id', // optional: defaults to 'PK'
'endpoint' => 'http://localhost:8000', // optional: used for local or when using DAX
'region' => '<region>', // optional: defaults to AWS_REGION env var
'ttl' => 60*60*24, // optional: number of seconds until items are considered expired
'ttlAttribute' => 'expires' // optional: defaults to 'TTL'
'credentials' => [
'key' => '<key>', // optional: defaults to AWS_ACCESS_KEY_ID env var
'secret' => '<secret>', // optional: defaults to AWS_SECRET_ACCESS_KEY env var
],
],
],
],
];
Expand All @@ -122,7 +134,7 @@ aws dynamodb create-table --table-name=my-app-queue-table \
In your `app.php`, configure the `queue` component to use the driver.

```php
use \pixelandtonic\dynamodb\drivers\DynamoDbQueue;
use craftcms\dynamodb\DynamoDbQueue;

return [
'bootstrap' => [
Expand All @@ -131,13 +143,18 @@ return [
'components' => [
'queue' => [
'class' => DynamoDbQueue::class,
'table' => 'my-app-queue-table',
'tableIdAttribute' => 'id', // optional: defaults to id
'tableDataAttribute' => 'data', // optional: defaults to data
'endpoint' => 'http://localhost:8000', // optional: used for local or when using DAX
'key' => '<key>', // optional: defaults to AWS_ACCESS_KEY_ID env var
'secret' => '<secret>', // optional: defaults to AWS_SECRET_ACCESS_KEY env var
'region' => '<region>', // optional: defaults to AWS_REGION env var
'dynamoDb' => [
'table' => 'my-app-queue-table',
'partitionKeyAttribute' => 'id', // optional: defaults to 'PK'
'endpoint' => 'http://localhost:8000', // optional: used for local or when using DAX
'region' => '<region>', // optional: defaults to AWS_REGION env var
'ttl' => 60*60*24, // optional: number of seconds until items are considered expired
'ttlAttribute' => 'expires' // optional: defaults to 'TTL'
'credentials' => [
'key' => '<key>', // optional: defaults to AWS_ACCESS_KEY_ID env var
'secret' => '<secret>', // optional: defaults to AWS_SECRET_ACCESS_KEY env var
],
],
],
],
];
Expand All @@ -152,14 +169,15 @@ Tests run against local DynamoDB tables using Docker. To run tests, you must run
3. Create the DynamoDB tables for the [cache](#create-dynamodb-cache-table), [session](#create-dynamodb-session-table), and [queue](#create-dynamodb-queue-table)
4. Run the test suite with `vendor/bin/phpunit --testdox`

To make the setup and testing easier, you can run the following Composer scripts:
To make the setup and testing easier, you can run the following Composer scripts:

1. `composer run setup`
2. `composer run test`

## Credits

- [Jason McCallister](https://github.com/jasonmccallister)
- [Tim Kelty](https://github.com/timkelty)
- [All Contributors](../../contributors)

## License
Expand Down
Loading