Skip to content

Commit

Permalink
支持指定使用的队列连接;
Browse files Browse the repository at this point in the history
修复指定队列无效的bug;
  • Loading branch information
whosphp committed Apr 23, 2020
1 parent 3a0b837 commit d07956c
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 76 deletions.
19 changes: 12 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This product is publicly available under the terms of the MIT license included i
## Installation and Requirements
First, you'll need to require the package with Composer:
```
composer require unikent/tfilecache
composer require whosphp/tfilecache
```

Then, update `config/app.php` by adding an entry for the service provider.
Expand All @@ -23,19 +23,24 @@ Finally, add the necessary config to `config\cache.php`.
'default' => env('CACHE_DRIVER', 'tfile'),
'stores' => [
'tfile' => [
'driver' => 'tfile',
'path' => storage_path('framework/cache')
],
// ...
'tfile' => [
'driver' => 'tfile',
'path' => storage_path('framework/cache'),
'connection' => null,
'queue' => null,
'separator' => null,
],
// ...
],
```

## Optional Configuration
There are some optional config options available in the store definition above:

`connection` : accepts the string name of a queue connection to use for [tag clean up](#tag-cleanup), will use the default queue connection if omitted.

`queue` : accepts the string name of a queue to use for [tag clean up](#tag-cleanup), will use the default queue if omitted.

`separator` : defines the separator character or sequence to be used internally, this should be chosen to **never** collide with a key value. defaults to `~#~` if omitted.


Expand Down
82 changes: 45 additions & 37 deletions src/Cache/FileTagSet.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,53 @@
<?php

namespace Unikent\Cache;

use Illuminate\Cache\TagSet;
use Unikent\Jobs\FlushTagFromFileCacheJob;

class FileTagSet extends TagSet{

protected static $driver = 'tfile';

/**
* Get the tag identifier key for a given tag.
*
* @param string $name
* @return string
*/
public function tagKey($name)
{
return 'cache_tags' . $this->store->separator . preg_replace('/[^\w\s\d\-_~,;\[\]\(\).]/', '~', $name);
}


/**
* Reset the tag and return the new tag identifier.
*
* @param string $name
* @return string
*/
public function resetTag($name)
{

$oldID = $this->store->get($this->tagKey($name));

if ($oldID!==false){
$job = new FlushTagFromFileCacheJob($oldID, static::$driver);
if(!empty($this->store->queue)){
$job->onQueue($this->store->queue);
}
dispatch($job);
}

return parent::resetTag($name);
}
class FileTagSet extends TagSet
{

protected static $driver = 'tfile';

/**
* Get the tag identifier key for a given tag.
*
* @param string $name
* @return string
*/
public function tagKey($name)
{
return 'cache_tags' . $this->store->separator . preg_replace('/[^\w\s\d\-_~,;\[\]\(\).]/', '~', $name);
}


/**
* Reset the tag and return the new tag identifier.
*
* @param string $name
* @return string
*/
public function resetTag($name)
{

$oldID = $this->store->get($this->tagKey($name));

if ($oldID !== false) {
$job = new FlushTagFromFileCacheJob($oldID, static::$driver);

if (!empty($this->store->connection)) {
$job->onConnection($this->store->connection);
}

if (!empty($this->store->queue)) {
$job->onQueue($this->store->queue);
}

dispatch($job);
}

return parent::resetTag($name);
}

}
19 changes: 11 additions & 8 deletions src/Cache/TaggableFileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class TaggableFileStore extends FileStore
{

public $separator;
protected $queue;
public $connection;
public $queue;

/**
* Create a new file cache store instance.
Expand All @@ -21,15 +22,17 @@ class TaggableFileStore extends FileStore
*/
public function __construct(Filesystem $files, $directory, $options)
{
$defaults = [
'separator'=> '~#~',
'queue' => null
];
$defaults = [
'separator'=> '~#~',
'connection' => null,
'queue' => null
];

$options = array_merge($defaults,$options);
$options = array_merge($defaults,$options);

$this->separator = $options['separator'];
$this->queue = $options['queue'];
$this->separator = $options['separator'];
$this->connection = $options['connection'];
$this->queue = $options['queue'];

parent::__construct($files,$directory);
}
Expand Down
48 changes: 24 additions & 24 deletions src/Jobs/FlushTagFromFileCacheJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@

class FlushTagFromFileCacheJob implements ShouldQueue
{
/*
|--------------------------------------------------------------------------
| Queueable Jobs
|--------------------------------------------------------------------------
|
| This job base class provides a central location to place any logic that
| is shared across all of your jobs. The trait included with the class
| provides access to the "queueOn" and "delay" queue helper methods.
|
*/
/*
|--------------------------------------------------------------------------
| Queueable Jobs
|--------------------------------------------------------------------------
|
| This job base class provides a central location to place any logic that
| is shared across all of your jobs. The trait included with the class
| provides access to the "queueOn" and "delay" queue helper methods.
|
*/

use InteractsWithQueue, Queueable, SerializesModels;
use InteractsWithQueue, Queueable, SerializesModels;

protected $tagIds;
protected $driver;
protected $tagIds;
protected $driver;

/**
* Create a new job instance.
*
* @param $ids array of tagIds to find and purge
*/
public function __construct( $ids , $driver = 'tfile')
/**
* Create a new job instance.
*
* @param $ids array of tagIds to find and purge
*/
public function __construct($ids, $driver = 'tfile')
{
$this->tagIds = is_array($ids)?$ids:[$ids];
$this->driver = $driver;
$this->tagIds = is_array($ids) ? $ids : [$ids];
$this->driver = $driver;
}

/**
Expand All @@ -43,8 +43,8 @@ public function __construct( $ids , $driver = 'tfile')
*/
public function handle()
{
foreach($this->tagIds as $id){
app('cache')->driver($this->driver)->flushOldTag($id);
}
foreach ($this->tagIds as $id) {
app('cache')->driver($this->driver)->flushOldTag($id);
}
}
}

0 comments on commit d07956c

Please sign in to comment.