Skip to content

Symfony cache lock strategy #51

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
UPGRADE
=======

- [2.0.0](#2.0.0)
- [1.2.0](#1.2.0)
- [1.1.0](#1.1.0)
- [0.4.0](#0.4.0)

### 2.0.0 (unreleased)

For the utf8mb4 compatibility with mysql some fields need to be shorten:

```bash
bin/console doctrine:schema:update
```

### 1.2.0

In the database table `ta_task_executions` a new field was introduced. Run following
Expand Down
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"symfony/config": "^2.8 || ^3.4 || ^4.0",
"symfony/console": "^2.8 || ^3.4 || ^4.0",
"symfony/process": "^2.8 || ^3.4 || ^4.0",
"doctrine/orm": "^2.5"
"doctrine/orm": "^2.5",
"symfony/cache": " ^2.8 || ^3.4 || ^4.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.0",
Expand All @@ -39,5 +40,11 @@
"psr-4": {
"Task\\TaskBundle\\Tests\\": "tests"
}
},
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-develop": "2.0-dev"
}
}
}
79 changes: 79 additions & 0 deletions src/Locking/CacheLockStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of php-task library.
*
* (c) php-task
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Task\TaskBundle\Locking;

use Psr\Cache\CacheItemInterface;
use Psr\Cache\InvalidArgumentException;
use Symfony\Contracts\Cache\CacheInterface;
use Task\Lock\LockStorageInterface;

/**
* Save locks in Symfony Cache.
*/
class CacheLockStorage implements LockStorageInterface
{

/**
* Avoid key collision with a key prefix.
*/
const KEY_PREFIX = 'php_task_lock__';

/**
* @var CacheInterface
*/
protected $cache;

/**
* @param CacheInterface $cache
*/
public function __construct($cache)
{
$this->cache = $cache;
}

/**
* {@inheritdoc}
*/
public function save($key, $ttl)
{
/** @var CacheItemInterface $cacheItem */
$cacheItem = $this->cache->getItem(self::KEY_PREFIX . $key);
$cacheItem->set('LOCK');
$cacheItem->expiresAfter($ttl);
$this->cache->save($cacheItem);
return true;
}

/**
* {@inheritdoc}
*/
public function delete($key)
{
try {
return $this->cache->deleteItem(self::KEY_PREFIX . $key);
} catch (InvalidArgumentException $e) {
return false;
}
}

/**
* {@inheritdoc}
*/
public function exists($key)
{
/** @var CacheItemInterface $cacheItem */
$cacheItem = $this->cache->getItem(self::KEY_PREFIX . $key);
return $cacheItem->isHit();
}

}

2 changes: 1 addition & 1 deletion src/Resources/config/doctrine/Task.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<field name="intervalExpression" type="string" length="255" nullable="true"/>
<field name="firstExecution" type="datetime" nullable="true"/>
<field name="lastExecution" type="datetime" nullable="true"/>
<field name="systemKey" type="string" nullable="true" unique="true"/>
<field name="systemKey" type="string" nullable="true" unique="true" length="191"/>
<field name="workload" type="object"/>

</entity>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/locking/storages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@

<tag name="task.lock.storage" alias="file"/>
</service>
<service id="task.lock.storage.cache" class="Task\TaskBundle\Locking\CacheLockStorage" public="true">
<argument type="service" id="cache.app"></argument>
<tag name="task.lock.storage" alias="cache"/>
</service>
</services>
</container>