galileo_redis 3.0.0
galileo_redis: ^3.0.0 copied to clipboard
An Galileo service provider for Redis. Works well for caching volatile data.
redis #
Redis-enabled services for the Galileo 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:galileo_redis requires Galileo 2.
In your pubspec.yaml:
dependencies:
galileo_framework: ^2.0.0-alpha
galileo_redis: ^1.0.0
Usage #
Pass an instance of RespCommands (from package:resp_client) to the RedisService constructor.
You can also pass an optional prefix, which is recommended if you are using galileo_redis for multiple
logically-separate collections. Redis is a flat key-value store; by prefixing the keys used,
galileo_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.
Notes #
- Neither
index, normodifyis atomic; each performs two separate queries.galileo_redisstores data as JSON strings, rather than as Redis hashes, so an update-in-place is impossible. indexuses Redis'KEYSfunctionality, so use it sparingly in production, if at all. In a larger database, it can quickly become a bottleneck.removeusesMULTI+EXECin a transaction.- Prefer using
update, rather thanmodify. The former only performs one query, though it does overwrite the current contents for a given key. - When calling
create, it's possible that you may already have anidin mind to insert into the store. For example, when caching another database, you'll preserve the ID or primary key of an item.galileo_redisheeds this. If noidis present, then an ID will be created via anINCRcall.
Example #
Also present at example/main.dart:
import 'package:galileo_redis/galileo_redis.dart';
import 'package:resp_client/resp_client.dart';
import 'package:resp_client/resp_commands.dart';
main() async {
var connection = await connectSocket('localhost');
var client = new RespClient(connection);
var service = new RedisService(new RespCommands(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();
}