2017 © Pedro Peláez
 

library redisun

Make redis manipulations easy. Unify commands for all data types.

image

limen/redisun

Make redis manipulations easy. Unify commands for all data types.

  • Friday, June 22, 2018
  • by limen
  • Repository
  • 4 Watchers
  • 19 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 9 Forks
  • 2 Open issues
  • 13 Versions
  • 0 % Grown

The README.md

Make redis manipulations easy. Unify commands for all data types.

Build Status Packagist, (*1)

中文, (*2)

Wiki, (*3)

Python version, (*4)

Features

  • Unified commands for all data types: string, list, hash, set and zset.
  • support SQL like query
  • use "eval" to save time consumption on network.
  • "set" like commands all support to set new ttl or keep current ttl

Unified commands

  • create: create key
  • createNotExists: create key when which not exists
  • createExists: create key when which exists
  • insert: similar to create except supporting multiple keys
  • insertNotExists: similar to createNotExists
  • insertExists: similar to createExists
  • get: get key to replace get, lrange, hgetall, smembers and zrange
  • getAndSet: get key and set new value
  • find: similar to get
  • findBatch: find batch
  • update: update keys
  • destroy: remove one key
  • destroyBatch: remove keys
  • delete: remove keys

Installation

Recommend to install via composer., (*5)

composer require "limen/redisun"

Usage

use Limen\Redisun\Examples\HashModel;
use Limen\Redisun\Examples\StringModel;

$person = [
   'name' => 'martin',
   'age' => '22',
   'height' => '175',
   'nation' => 'China',
];
$hashModel = new HashModel();
$hashModel->create(1, $person);
$hashModel->find(1);                    // return $person
$hashModel->where('id',1)->first();     // return $person
$hashModel->where('id',1)->get();       // return ['redisun:1:hash' => $person]
$hashModel->where('id',1)->delete();    // remove key "redisun:1:hash" from database

$nick = 'martin-walk';

$stringModel = new StringModel();
$stringModel->insert([
    'id' => 1,
    'name' => 'martin'
], $nick);
$stringModel->where('id',1)->first();   // return $nick
$stringModel->where('id',1)->get();     // return ['redisun:1:string:martin' => $nick]

Concepts

Key representation

Every model has its own key representation which tells how to build query keys. For example, (*6)

school:{schoolId}:class:{classId}:members

We can use where clauses to query the Redis., (*7)

$model->where('schoolId',1)->whereIn('classId',[1,2])->get();

The keys to query are, (*8)

school:1:class:1:members
school:1:class:2:members

Key field

Key field is a dynamic part of the key representation., (*9)

Take the key representation above, it has two fields, (*10)

  • schoolId
  • classId

Complete key

When a key has no unbound field, we treat it as complete. For example, (*11)

school:1:class:2:members

On the contrary, an incomplete key is similar to, (*12)

school:1:class:{classId}:members

Returned data set

The returned data set would be an associated array whose indices are the query keys., (*13)

When both keys exist on Redis database, the returned data set would be, (*14)

[
    'school:1:class:1:members' => <item1>,
    'school:1:class:2:members' => <item2>,
]

If a key not exist, the equivalent index would be not set., (*15)

The returned item's data type depends on the model's type which could be string, hash, list, set or zset., (*16)

  • string: string
  • hash: associated array
  • list: array
  • set: array
  • zset: array

Methods

create

Can use when a model's key representation has only one dynamic field as its primary field., (*17)

The item's ttl is optional., (*18)

Hash type with key representation, (*19)

user:{id}:info
$model->create(1, [
    'name' => 'maria',
    'age' => 22,
], 10);   // the item "user:1:info" would expire after 10 seconds

zset type with key representation, (*20)

shop:{id}:customers
// key -> member, value -> score
$model->create(1, [
    'maria' => 1,
    'martin' => 2,
]);   // the item "shop:1:customers" would not expire

createExists

Similar to "setxx" but supports more data types: string, hash, set, zset and list., (*21)

createNotExists

Similar to "setnx" but supports more data types., (*22)

insert

An optional parameter make it possible to insert like "setnx" and "setxx". String type with key representation., (*23)

user:{id}:code
$model->insert([
    'id' => 1,
], 10010, 20); // the item "user:1:code" would expire after 20 seconds 

insertExists

Similar to createExists, (*24)

insertNotExists

Similar to createNotExists, (*25)

find

Can use when a model's key representation has only one dynamic field as its primary field., (*26)

$model->find(1);

findBatch

Similar to find. The returned data set are indexed by ids., (*27)

$model->findBatch([1,2,3]);
// [
//     1 => <item1>,
//     2 => <item2>,
//     3 => <item3>,
// ]

updateBatch

Similar to findBatch., (*28)

The key would be created if not exist. The key's ttl would not be modified if the ttl parameter not set., (*29)

$model->updateBatch([1,2,3], $value);

all

key representation, (*30)

user:{id}:code
$model->all();      // return all keys which match pattern "user:*:code"

where

Similar to SQL, (*31)

$model->where('id', 1)->where('name', 'maria');

whereIn

Similar to where, (*32)

$model->whereIn('id', [1,2,3]);

first

Get first exist item from query keys. Return null when all query keys not exist., (*33)

$model->whereIn('id', [1,2,3])->first();    // return string|array|null

update

The key would be created if not exist. The key's ttl would not be modified if the ttl parameter not set., (*34)

$model->where('id',1)->update($value);

delete

Delete query keys., (*35)

$model->where('id',1)->delete();

orderBy, sort

string type with key representation, (*36)

user:{id}:code
$model->insert([
    'id' => 1,
], 10010); 
$model->insert([
    'id' => 2,
], 10011); 

$model->whereIn('id', [1,2])->orderBy('id')->get();
// returned data set
// [
//     'user:1:code' => 10010,
//     'user:2:code' => 10011,
// ]
$model->newQuery()->whereIn('id', [1,2])->orderBy('id', 'desc')->get();
// returned data set
// [
//     'user:2:code' => 10011,
//     'user:1:code' => 10010,
// ]
$model->newQuery()->whereIn('id', [1,2])->sort();
// returned data set
// [
//     'user:1:code' => 10010,
//     'user:2:code' => 10011,
// ]

count

Count the exist query keys., (*37)

$model->where('id', 1)->count();    // return an integer

max

Get the maximum item in the returned data set., (*38)

$model->where('id', 1)->max();

min

Get the minimum item in the returned data set., (*39)

$model->where('id', 1)->min();

sum

Get the sum of the returned data set., (*40)

$model->where('id', 1)->sum();

Predis native methods

Predis native methods such as "sadd", "hset" can use when the query contains only one complete query key., (*41)

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

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

Query builder

Taking the job to build query keys for model., (*42)

key representation, (*43)

user:{id}:{name}
$queryBuilder->whereIn('id', [1,2])->whereIn('name', ['maria', 'cat']);
// built keys
// user:1:maria
// user:1:cat
// user:2:maria
// user:2:cat

$queryBuilder->refresh()->whereIn('id', [1,2]);
// built keys
// user:1:{name}
// user:2:{name}

Development

Test

$ phpunit --bootstrap tests/bootstrap.php tests/

The Versions

22/06 2018

dev-master

9999999-dev https://github.com/limen/redisun

Make redis manipulations easy. Unify commands for all data types.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql redis eval lua

18/06 2018

v2.0.0

2.0.0.0 https://github.com/limen/redisun

Make redis manipulations easy. Unify commands for all data types.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql redis eval lua

10/06 2018

v1.0.5

1.0.5.0 https://github.com/limen/redmodel

CURD model for Redis in laravel style.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql redis model

10/06 2018

v1.0.4

1.0.4.0 https://github.com/limen/redmodel

CURD model for Redis in laravel style.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql redis model

08/06 2017

v1.0.3

1.0.3.0 https://github.com/limen/redmodel

CURD model for Redis in laravel style.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql redis model

01/04 2017

v1.0.2

1.0.2.0 https://github.com/limen/redmodel

CURD model for Redis in laravel style.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql redis model

30/03 2017

v1.0.1

1.0.1.0 https://github.com/limen/redmodel

CURD model for Redis in laravel style.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel redis model

30/03 2017

v1.0.0

1.0.0.0 https://github.com/limen/redmodel

CURD model for Redis in laravel style.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel redis model

10/03 2017

v0.1.3

0.1.3.0 https://github.com/limen/redmodel

CURD model for Redis in laravel style.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel redis model

19/12 2016

v0.1.0.x-dev

0.1.0.9999999-dev https://github.com/limen/redmodel

CURD model for Redis in laravel style.

  Sources   Download

MIT

The Requires

 

laravel redis model

19/12 2016

v0.1.2

0.1.2.0 https://github.com/limen/redmodel

CURD model for Redis in laravel style.

  Sources   Download

MIT

The Requires

 

laravel redis model

09/12 2016

v0.1.1

0.1.1.0 https://github.com/limen/redmodel

CURD model for Redis in laravel style.

  Sources   Download

MIT

The Requires

 

laravel redis model

08/12 2016

v0.1.0

0.1.0.0 https://github.com/limen/redmodel

CURD model for Redis in laravel style.

  Sources   Download

MIT

The Requires

 

laravel redis model