PHPackages                             limen/redmodel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. limen/redmodel

ActiveLibrary[Database &amp; ORM](/categories/database)

limen/redmodel
==============

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

v2.0.3(7y ago)3837012[1 issues](https://github.com/limen/redisun/issues)2MITPHPPHP &gt;=5.5CI failing

Since Dec 8Pushed 6y ago4 watchersCompare

[ Source](https://github.com/limen/redisun)[ Packagist](https://packagist.org/packages/limen/redmodel)[ Docs](https://github.com/limen/redisun)[ RSS](/packages/limen-redmodel/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (2)Versions (16)Used By (2)

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

[](#make-redis-manipulations-easy-unify-commands-for-all-data-types)

[![Build Status](https://camo.githubusercontent.com/ab72ada47e8d48df2c057c0ec4afcd2b1fd57d408e28440a83c1c9136ec5aa53/68747470733a2f2f7472617669732d63692e6f72672f6c696d656e2f7265646973756e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/limen/redisun)[![Packagist](https://camo.githubusercontent.com/0b3b5913667790919b7217745cf70585c5f1195ec18f5de9eaed5fa355240b0b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c696d656e2f7265646973756e2e7376673f6d61784167653d32353932303030)](https://packagist.org/packages/limen/redisun)

[中文](https://github.com/limen/redisun/blob/master/README.cn.md)

[Wiki](https://github.com/limen/redisun/wiki)

[Python version](https://github.com/limen/redisun-py)

Features
--------

[](#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
----------------

[](#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
------------

[](#installation)

Recommend to install via [composer](https://getcomposer.org/).

```
composer require "limen/redisun"
```

Usage
-----

[](#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
--------

[](#concepts)

#### *Key representation*

[](#key-representation)

Every model has its own key representation which tells how to build query keys. For example

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

```

We can use where clauses to query the Redis.

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

```

The keys to query are

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

```

#### *Key field*

[](#key-field)

Key field is a dynamic part of the key representation.

Take the key representation above, it has two fields

- schoolId
- classId

#### *Complete key*

[](#complete-key)

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

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

```

On the contrary, an incomplete key is similar to

```
school:1:class:{classId}:members

```

Returned data set
-----------------

[](#returned-data-set)

The returned data set would be an associated array whose indices are the query keys.

When both keys exist on Redis database, the returned data set would be

```
[
    'school:1:class:1:members' => ,
    'school:1:class:2:members' => ,
]

```

If a key not exist, the equivalent index would be not set.

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

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

Methods
-------

[](#methods)

### create

[](#create)

Can use when a model's key representation has only one dynamic field as its primary field.

The item's ttl is optional.

Hash type with key representation

```
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

```
shop:{id}:customers

```

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

```

### createExists

[](#createexists)

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

### createNotExists

[](#createnotexists)

Similar to "setnx" but supports more data types.

### insert

[](#insert)

An optional parameter make it possible to insert like "setnx" and "setxx". String type with key representation.

```
user:{id}:code

```

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

```

### insertExists

[](#insertexists)

Similar to createExists

### insertNotExists

[](#insertnotexists)

Similar to createNotExists

### find

[](#find)

Can use when a model's key representation has only one dynamic field as its primary field.

```
$model->find(1);

```

### findBatch

[](#findbatch)

Similar to find. The returned data set are indexed by ids.

```
$model->findBatch([1,2,3]);
// [
//     1 => ,
//     2 => ,
//     3 => ,
// ]

```

### updateBatch

[](#updatebatch)

Similar to findBatch.

The key would be created if not exist. The key's ttl would not be modified if the ttl parameter not set.

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

```

### all

[](#all)

key representation

```
user:{id}:code

```

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

```

### where

[](#where)

Similar to SQL

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

```

### whereIn

[](#wherein)

Similar to where

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

```

### first

[](#first)

Get first exist item from query keys. Return null when all query keys not exist.

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

```

### update

[](#update)

The key would be created if not exist. The key's ttl would not be modified if the ttl parameter not set.

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

```

### delete

[](#delete)

Delete query keys.

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

```

### orderBy, sort

[](#orderby-sort)

string type with key representation

```
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)

Count the exist query keys.

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

```

### max

[](#max)

Get the maximum item in the returned data set.

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

```

### min

[](#min)

Get the minimum item in the returned data set.

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

```

### sum

[](#sum)

Get the sum of the returned data set.

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

```

Predis native methods
---------------------

[](#predis-native-methods)

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

```
// 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
-------------

[](#query-builder)

Taking the job to build query keys for model.

key representation

```
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
-----------

[](#development)

### Test

[](#test)

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

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~51 days

Recently: every ~43 days

Total

15

Last Release

2723d ago

Major Versions

v0.1.3 → v1.0.02017-03-30

v1.0.5 → v2.0.02018-06-18

PHP version history (2 changes)v0.1.3PHP &gt;=5.4

v2.0.0PHP &gt;=5.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/49d0b6c3647be78ab9b2703cd07c681a8098a0815f2ac676da4e3eafd2c08474?d=identicon)[limen](/maintainers/limen)

---

Top Contributors

[![limen](https://avatars.githubusercontent.com/u/3460683?v=4)](https://github.com/limen "limen (40 commits)")

---

Tags

evalluaredissqlormsqlredisevallua

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/limen-redmodel/health.svg)

```
[![Health](https://phpackages.com/badges/limen-redmodel/health.svg)](https://phpackages.com/packages/limen-redmodel)
```

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[limen/redisun

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

381.7k](/packages/limen-redisun)[cycle/orm

PHP DataMapper ORM and Data Modelling Engine

1.3k835.4k65](/packages/cycle-orm)[matthiasmullie/scrapbook

Scrapbook is a PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APCu, SQL and additional capabilities (e.g. transactions, stampede protection) built on top.

3212.5M32](/packages/matthiasmullie-scrapbook)[nilportugues/sql-query-builder

An elegant lightweight and efficient SQL QueryInterface BuilderInterface supporting bindings and complicated query generation.

425239.4k6](/packages/nilportugues-sql-query-builder)[atlas/orm

An ORM for your persistence model (not your domain model).

429139.8k12](/packages/atlas-orm)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
