angel3_redis 10.0.0
angel3_redis: ^10.0.0 copied to clipboard
An Angel3 service provider for Redis. Works well for caching volatile data.
Angel3 Redis #
Forked from angel_redis to support NNBD
Redis-enabled services for the Angel3 framework. RedisService can be used alone, or as the backend of a CacheService, and thereby cache the results of calling an upstream database.
Installation #
package:angel3_redis requires Angel3.
In your pubspec.yaml:
dependencies:
angel3_framework: ^10.0.0
angel3_redis: ^10.0.0
Usage #
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 angel3_redis for multiple logically-separate collections. Redis is a flat key-value store; by prefixing the keys used, angel3_redis can provide the experience of using separate stores, rather than a single node.
Without a prefix, there's a chance that different collections can overwrite one another's data.
Example #
Also present at example/main.dart:
import 'package:angel3_redis/angel3_redis.dart';
import 'package:resp_client/resp_client.dart';
import 'package:resp_client/resp_commands.dart';
import 'package:resp_client/resp_server.dart';
main() async {
var connection = await connectSocket('localhost');
var client = RespClient(connection);
var service = RedisService(RespCommandsTier2(client), prefix: 'example');
// 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();
}
Notes #
angel3_redisstores each record as a JSON string, not as a Redis hash. Keys that hold anything other than a JSON object are not records:indexskips them, andread,modifyandremovereturn 404 for them.- Prefixes must not nest.
indexon prefixappalso returns the records of a service with prefixapp:users. Without a prefix,indexscans the whole database. indexusesSCANandMGETin batches ofbatchSizekeys (default 1000), so it does not block the Redis server, but it still reads every record in the collection.modifyandremoveare atomic. They read the record, then write only if it has not changed in the meantime, trying up tomaxWriteAttemptstimes (default 10). If the record keeps changing, they return 409.- Prefer
updatetomodifywhen you have the whole record. It performs one query and overwrites the stored record. createkeeps anidyou supply, for example the primary key of a row you are caching from another database. Theidmust be a non-empty string or an integer, otherwisecreatereturns 400. If a record with thatidalready exists,createreturns 409 instead of overwriting it.- Without an
id,creategenerates one by incrementing the counterangel_redis:id:<prefix>(angel_redis:idwithout a prefix), skipping IDs that are already taken. On a database used with 9.x, the counter continues from the old<prefix>:angel_redis:idkey, which is left in place.
Migrating from 9.x to 10.0.0 #
The breaking changes of each release are listed in the CHANGELOG. Those of 10.0.0 that may need changes in an app:
createthrows a409if a record of the id already exists, and a400unless the id is a non-empty string or an integer. Useupdateto replace a record.modifyandremovethrow a409if the record keeps changing during their writes (maxWriteAttempts).read,modifyandremovethrow a404for keys that do not hold a JSON object.