CURD model for Redis in Laravel style

Features

  • Supported operations: create, insert, find, destroy and so on
  • Fluent query builder
  • Use “multi” and “exec” for batch operation

Installation

This library could be found on Packagist for an easier management of projects dependencies using Composer.

Github repo: redmodel

Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use Limen\RedModel\Examples\HashModel;
// constructing parameters are passed transparently to Predis client's constructor
$hashModel = new HashModel([
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
]);
$maria = [
'name' => 'Maria',
'age' => '22',
'nation' => 'USA',
'state' => 'New York',
];
$cat = [
'name' => 'Catherine',
'age' => '23',
'nation' => 'UK',
'city' => 'London',
];
$tested = [];
// insert
$hashModel->insert(['id' => 1], $maria);
$hashModel->insert(['id' => 2], $cat);
// find by primary key
$user = $hashModel->find(1);
if ($user === $maria) {
$tested[] = 'Find OK';
}
// find by query
$users = $hashModel->where('id', 1)->get();
if ($users === [$maria]) {
$tested[] = 'Where then get OK';
}
$user = $hashModel->where('id', 1)->first();
if ($user === $maria) {
$tested[] = 'Where then first OK';
}
$users = $hashModel->whereIn('id', [1,2])->get();
if ($users === [$maria, $cat]) {
$tested[] = 'Where in then get OK';
}
// find batch by primary keys
$users = $hashModel->findBatch([1,2]);
if ($users === [$maria, $cat]) {
$tested[] = 'find batch OK';
}
// update by query
$hashModel->where('id', 1)->update([
'age' => '23',
]);
$user = $hashModel->find(1);
if ($user['age'] === '23') {
$tested[] = 'Update OK';
}
// remove item
$hashModel->destroy(1);
$user = $hashModel->find(1);
if (!$user) {
$tested[] = 'Destroy OK';
}
var_dump($tested);

Operation notices

create

Can use when a model’s key representation has only one dynamic field.

The default choice is “forced”, which would replace the same key if exists.

insert

The default choice is “forced”, which would replace the same key if exists.

Redis native methods

Redis native methods such as “set”, “hmset” can use when the query builder contains only one valid query key.

// string model
$model->where('id', 1)->set('maria');

// hash model
$model->where('id', 1)->update([
    'name' => 'Maria',
    'age' => '22',
]);
// equal to
$model->where('id', 1)->hmset([
    'name' => 'Maria',
    'age' => '22',
]);

Query builder

Taking the job to build query keys for model.

1
2
3
4
5
// model's key representation user:{id}:{name}
$queryBuilder->whereIn('id', [1,2])->where('name', 'maria');
// built keys
// user:1:maria
// user:2:maria

The built query keys which contain unbound fields would be ignored. For example

1
user:1:{name}