2024-10-12 10:35:14 +00:00
# Protevus Redis
2018-10-21 18:18:41 +00:00
2024-10-13 01:45:27 +00:00
![Pub Version (including pre-releases) ](https://img.shields.io/pub/v/protevus_redis?include_prereleases )
2021-06-21 01:58:05 +00:00
[![Null Safety ](https://img.shields.io/badge/null-safety-brightgreen )](https://dart.dev/null-safety)
2024-07-07 15:02:49 +00:00
[![Discord ](https://img.shields.io/discord/1060322353214660698 )](https://discord.gg/3X6bxTUdCM)
2024-10-12 10:35:14 +00:00
[![License ](https://img.shields.io/github/license/dart-backend/protevus )](https://github.com/dart-backend/protevus/tree/master/packages/redis/LICENSE)
2021-06-21 01:58:05 +00:00
**Forked from `angel_redis` to support NNBD**
2024-10-13 01:45:27 +00:00
Redis-enabled services for the Protevus framework. `RedisService` can be used alone, *or* as the backend of a [`CacheService` ](https://pub.dev/packages/protevus_cache ), and thereby cache the results of calling an upstream database.
2018-10-21 18:04:01 +00:00
## Installation
2021-06-21 01:58:05 +00:00
2024-10-13 01:45:27 +00:00
`package:protevus_redis` requires Protevus.
2018-10-21 18:19:58 +00:00
2018-10-21 18:04:01 +00:00
In your `pubspec.yaml` :
```yaml
dependencies:
2024-10-13 01:45:27 +00:00
protevus_framework: ^8.0.0
protevus_redis: ^8.0.0
2018-10-21 18:04:01 +00:00
```
## Usage
2021-06-21 01:58:05 +00:00
2024-10-13 01:45:27 +00:00
Pass an instance of `RespCommandsTier2` (from `package:resp_client` ) to the `RedisService` constructor. You can also pass an optional prefix, which is recommended if you are using `protevus_redis` for multiple logically-separate collections. Redis is a flat key-value store; by prefixing the keys used, `protevus_redis` can provide the experience of using separate stores, rather than a single node.
2018-10-21 18:04:01 +00:00
Without a prefix, there's a chance that different collections can overwrite one another's data.
## Notes
2021-06-21 01:58:05 +00:00
2024-10-13 01:45:27 +00:00
* Neither `index` , nor `modify` is atomic; each performs two separate queries.`protevus_redis` stores data as JSON strings, rather than as Redis hashes, so an update-in-place is impossible.
2018-10-21 18:04:01 +00:00
* `index` uses Redis' `KEYS` functionality, so use it sparingly in production, if at all. In a larger database, it can quickly
become a bottleneck.
* `remove` uses `MULTI` +`EXEC` in a transaction.
* Prefer using `update` , rather than `modify` . The former only performs one query, though it does overwrite the current
2018-10-21 18:15:38 +00:00
contents for a given key.
* When calling `create` , it's possible that you may already have an `id` in mind to insert into the store. For example,
2024-10-13 01:45:27 +00:00
when caching another database, you'll preserve the ID or primary key of an item. `protevus_redis` heeds this. If no
2018-10-21 18:15:38 +00:00
`id` is present, then an ID will be created via an `INCR` call.
## Example
2021-06-21 01:58:05 +00:00
2018-10-21 18:19:58 +00:00
Also present at `example/main.dart` :
2018-10-21 18:15:38 +00:00
```dart
2024-10-13 01:45:27 +00:00
import 'package:protevus_redis/protevus_redis.dart';
2018-10-21 18:15:38 +00:00
import 'package:resp_client/resp_client.dart';
import 'package:resp_client/resp_commands.dart';
2021-06-21 01:58:05 +00:00
import 'package:resp_client/resp_server.dart';
2018-10-21 18:15:38 +00:00
main() async {
var connection = await connectSocket('localhost');
2021-06-21 01:58:05 +00:00
var client = RespClient(connection);
var service = RedisService(RespCommandsTier2(client), prefix: 'example');
2018-10-21 18:15:38 +00:00
// Create an object
await service.create({'id': 'a', 'hello': 'world'});
// Read it...
var read = await service.read('a');
print(read['hello']);
// Delete it.
await service.remove('a');
// Close the connection.
await connection.close();
}
2021-06-21 01:58:05 +00:00
```