Skip to content

Commit 4942a4c

Browse files
committed
Stop using deprecated RP_PRIMARY and replace it with PRIMARY in MongoDB read preferences.
1 parent fb010ef commit 4942a4c

File tree

6 files changed

+42
-91
lines changed

6 files changed

+42
-91
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
"require": {
77
"php": "^8.1",
88
"illuminate/cache": "^10.0",
9-
"mongodb/laravel-mongodb": "^4.1"
9+
"mongodb/laravel-mongodb": "^4.1",
10+
"ext-mongodb": "*"
1011
},
1112
"require-dev": {
12-
"orchestra/testbench": "^8.0",
13-
"ext-mongodb": "*"
13+
"orchestra/testbench": "^8.0"
1414
},
1515
"license": "MIT",
1616
"authors": [

src/Console/Commands/MongodbCacheDropIndex.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,30 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Facades\DB;
7-
use \MongoDB\Driver\ReadPreference;
7+
use MongoDB\Driver\ReadPreference;
88

99
/**
1010
* Drop the indexes created by MongodbCacheIndex
1111
*/
1212
class MongodbCacheDropIndex extends Command
1313
{
1414

15-
/**
16-
* The name and signature of the console command.
17-
*
18-
* @var string
19-
*/
15+
/** The name and signature of the console command. */
2016
protected $signature = 'mongodb:cache:dropindex {index}';
2117

22-
/**
23-
* The console command description.
24-
*
25-
* @var string
26-
*/
18+
/** The console command description. */
2719
protected $description = 'Drops the passed index from the mongodb `cache` collection';
2820

29-
/**
30-
* Execute the console command.
31-
*
32-
* @return mixed
33-
*/
34-
public function handle()
21+
/** Execute the console command. */
22+
public function handle(): void
3523
{
3624
$cacheCollectionName = config('cache')['stores']['mongodb']['table'];
3725

3826
DB::connection('mongodb')->getMongoDB()->command([
3927
'dropIndexes' => $cacheCollectionName,
4028
'index' => $this->argument('index'),
4129
], [
42-
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY)
30+
'readPreference' => new ReadPreference(ReadPreference::PRIMARY)
4331
]);
4432
}
4533
}

src/Console/Commands/MongodbCacheIndex.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,22 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Facades\DB;
7-
use \MongoDB\Driver\ReadPreference;
7+
use MongoDB\Driver\ReadPreference;
88

99
/**
1010
* Create indexes for the cache collection
1111
*/
1212
class MongodbCacheIndex extends Command
1313
{
1414

15-
/**
16-
* The name and signature of the console command.
17-
*
18-
* @var string
19-
*/
15+
/** The name and signature of the console command. */
2016
protected $signature = 'mongodb:cache:index';
2117

22-
/**
23-
* The console command description.
24-
*
25-
* @var string
26-
*/
18+
/** The console command description. */
2719
protected $description = 'Create indexes on the mongodb `cache` collection';
2820

29-
/**
30-
* Execute the console command.
31-
*
32-
* @return mixed
33-
*/
34-
public function handle()
21+
/** Execute the console command. */
22+
public function handle(): void
3523
{
3624
$cacheCollectionName = config('cache')['stores']['mongodb']['table'];
3725

@@ -52,7 +40,7 @@ public function handle()
5240
]
5341
]
5442
], [
55-
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY)
43+
'readPreference' => new ReadPreference(ReadPreference::PRIMARY)
5644
]);
5745
}
5846
}

src/Console/Commands/MongodbCacheIndexTags.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,22 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Facades\DB;
7-
use \MongoDB\Driver\ReadPreference;
7+
use MongoDB\Driver\ReadPreference;
88

99
/**
1010
* Create indexes for the cache collection
1111
*/
1212
class MongodbCacheIndexTags extends Command
1313
{
1414

15-
/**
16-
* The name and signature of the console command.
17-
*
18-
* @var string
19-
*/
15+
/** The name and signature of the console command. */
2016
protected $signature = 'mongodb:cache:index_tags';
2117

22-
/**
23-
* The console command description.
24-
*
25-
* @var string
26-
*/
18+
/** The console command description. */
2719
protected $description = 'Create indexes on the tags column of mongodb `cache` collection';
2820

29-
/**
30-
* Execute the console command.
31-
*
32-
* @return mixed
33-
*/
34-
public function handle()
21+
/** Execute the console command.*/
22+
public function handle(): void
3523
{
3624
$cacheCollectionName = config('cache')['stores']['mongodb']['table'];
3725

@@ -45,7 +33,7 @@ public function handle()
4533
],
4634
]
4735
], [
48-
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY)
36+
'readPreference' => new ReadPreference(ReadPreference::PRIMARY)
4937
]);
5038
}
5139
}

src/MongoTaggedCache.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
namespace ForFit\Mongodb\Cache;
44

5+
use Illuminate\Cache\Events\KeyWritten;
56
use Illuminate\Cache\Repository;
67
use Illuminate\Contracts\Cache\Store;
7-
use Illuminate\Cache\Events\KeyWritten;
88

99
class MongoTaggedCache extends Repository
1010
{
11-
protected $tags;
11+
protected array $tags;
1212

1313
/**
14-
* @param \Illuminate\Contracts\Cache\Store $store
14+
* @param Store $store
1515
* @param array $tags
1616
*/
1717
public function __construct(Store $store, array $tags = [])
@@ -27,12 +27,12 @@ public function __construct(Store $store, array $tags = [])
2727
* @param string $key
2828
* @param mixed $value
2929
* @param \DateTimeInterface|\DateInterval|float|int $ttl
30-
* @return void
30+
* @return bool|null
3131
*/
3232
public function put($key, $value, $ttl = null)
3333
{
3434
if (is_array($key)) {
35-
return $this->putMany($key, $value);
35+
$this->putMany($key, $value);
3636
}
3737

3838
$seconds = $this->getSeconds(is_null($ttl) ? 315360000 : $ttl);
@@ -45,19 +45,17 @@ public function put($key, $value, $ttl = null)
4545
}
4646

4747
return $result;
48-
} else {
49-
return $this->forget($key);
5048
}
49+
50+
return $this->forget($key);
5151
}
5252

5353
/**
5454
* Saves array of key value pairs to the cache
5555
*
56-
* @param array $values
5756
* @param \DateTimeInterface|\DateInterval|float|int $ttl
58-
* @return void
5957
*/
60-
public function putMany(array $values, $ttl = null)
58+
public function putMany(array $values, $ttl = null): void
6159
{
6260
foreach ($values as $key => $value) {
6361
$this->put($key, $value, $ttl);
@@ -66,11 +64,9 @@ public function putMany(array $values, $ttl = null)
6664

6765
/**
6866
* Flushes the cache for the given tags
69-
*
70-
* @return void
7167
*/
72-
public function flush()
68+
public function flush(): void
7369
{
74-
return $this->store->flushByTags($this->tags);
70+
$this->store->flushByTags($this->tags);
7571
}
7672
}

src/Store.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace ForFit\Mongodb\Cache;
44

55
use Closure;
6-
use Illuminate\Support\InteractsWithTime;
76
use Illuminate\Cache\RetrievesMultipleKeys;
8-
use Illuminate\Database\ConnectionInterface;
97
use Illuminate\Contracts\Cache\Store as StoreInterface;
8+
use Illuminate\Database\ConnectionInterface;
9+
use Illuminate\Support\InteractsWithTime;
1010
use Jenssegers\Mongodb\Query\Builder;
1111
use MongoDB\BSON\UTCDateTime;
1212
use MongoDB\Driver\Exception\BulkWriteException;
@@ -107,15 +107,15 @@ public function decrement($key, $value = 1)
107107
/**
108108
* @inheritDoc
109109
*/
110-
public function forever($key, $value)
110+
public function forever($key, $value): bool
111111
{
112112
return $this->put($key, $value, 315360000);
113113
}
114114

115115
/**
116116
* @inheritDoc
117117
*/
118-
public function forget($key)
118+
public function forget($key): bool
119119
{
120120
$this->table()->where('key', '=', $this->getPrefix() . $key)->delete();
121121

@@ -125,7 +125,7 @@ public function forget($key)
125125
/**
126126
* @inheritDoc
127127
*/
128-
public function flush()
128+
public function flush(): bool
129129
{
130130
$this->table()->delete();
131131

@@ -135,29 +135,23 @@ public function flush()
135135
/**
136136
* @inheritDoc
137137
*/
138-
public function getPrefix()
138+
public function getPrefix(): string
139139
{
140140
return $this->prefix;
141141
}
142142

143143
/**
144144
* Sets the tags to be used
145-
*
146-
* @param array $tags
147-
* @return MongoTaggedCache
148145
*/
149-
public function tags(array $tags)
146+
public function tags(array $tags): MongoTaggedCache
150147
{
151148
return new MongoTaggedCache($this, $tags);
152149
}
153150

154151
/**
155152
* Deletes all records with the given tag
156-
*
157-
* @param array $tags
158-
* @return void
159153
*/
160-
public function flushByTags(array $tags)
154+
public function flushByTags(array $tags): void
161155
{
162156
foreach ($tags as $tag) {
163157
$this->table()->where('tags', $tag)->delete();
@@ -166,11 +160,8 @@ public function flushByTags(array $tags)
166160

167161
/**
168162
* Retrieve an item's expiration time from the cache by key.
169-
*
170-
* @param string $key
171-
* @return null|float|int
172163
*/
173-
public function getExpiration($key)
164+
public function getExpiration($key): ?float
174165
{
175166
$cacheData = $this->table()->where('key', $this->getKeyWithPrefix($key))->first();
176167

@@ -199,7 +190,7 @@ protected function table()
199190
* @param string $key
200191
* @return string
201192
*/
202-
protected function getKeyWithPrefix(string $key)
193+
protected function getKeyWithPrefix(string $key): string
203194
{
204195
return $this->getPrefix() . $key;
205196
}
@@ -210,7 +201,7 @@ protected function getKeyWithPrefix(string $key)
210201
* @param string $key
211202
* @param int $value
212203
* @param Closure $callback
213-
* @return int|bool
204+
* @return false|mixed
214205
*/
215206
protected function incrementOrDecrement($key, $value, Closure $callback)
216207
{

0 commit comments

Comments
 (0)