typed_cache 0.1.0
typed_cache: ^0.1.0 copied to clipboard
A strongly-typed, generic caching library for Dart.
Typed Cache #
Um pacote Dart de cache type-safe com suporte a políticas de retenção (TTL), múltiplos backends de armazenamento e codificação flexível.
Features #
- 🔒 Type-safe: Cache com total suporte a tipos genéricos em Dart
- ⏰ TTL Policies: Controle de tempo de vida dos itens em cache com política configurável
- 🔌 Pluggable Backends: Suporte para múltiplos backends de armazenamento
- 🔄 Flexible Encoding: Codecs customizáveis para serialização/desserialização
- 🎯 Clean Architecture: Arquitetura modular e desacoplada
- 📝 Type Entries: Entrada tipada com metadados e timestamps
Getting started #
Pré-requisitos #
environment:
sdk: ^3.10.4
Instalação #
Adicione typed_cache ao seu pubspec.yaml:
flutter pub add typed_cache
# ou
dart pub add typed_cache
Usage #
Criando um Cache #
import 'package:typed_cache/typed_cache.dart';
// Criar um cache com TTL de 5 minutos
final cache = TypedCache<String, int>(
backend: InMemoryBackend(),
policy: TtlPolicy(duration: Duration(minutes: 5)),
);
// Armazenar um valor
await cache.set('counter', 42);
// Recuperar um valor
final value = await cache.get('counter');
print(value); // 42
// Remover um valor
await cache.remove('counter');
// Limpar todo o cache
await cache.clear();
Usando Diferentes Backends #
// Backend em memória (padrão)
final memoryCache = TypedCache<String, String>(
backend: InMemoryBackend(),
);
// Backend customizado
final customCache = TypedCache<String, User>(
backend: MyCustomBackend(),
codec: JsonCodec<User>(),
);
Políticas de TTL #
// TTL fixo de 1 hora
final ttlPolicy = TtlPolicy(duration: Duration(hours: 1));
// Usar com clock customizado (útil para testes)
final testPolicy = TtlPolicy(
duration: Duration(minutes: 5),
clock: FakeClock(),
);
Arquitetura #
typed_cache/
├── backend.dart # Interface abstrata para backends
├── cache_store.dart # Armazenamento interno
├── codec.dart # Codificação/descodificação
├── entry.dart # Entrada tipada com metadados
├── errors.dart # Exceções customizadas
├── typed_cache.dart # API principal
└── policy/
├── clock.dart # Interface de relógio (para testes)
└── ttl_policy.dart # Política de TTL
Contribuindo #
As contribuições são bem-vindas! Por favor:
- Faça um fork do repositório
- Crie uma branch para sua feature (
git checkout -b feature/amazing-feature) - Commit suas mudanças (
git commit -m 'Add amazing feature') - Push para a branch (
git push origin feature/amazing-feature) - Abra um Pull Request
Licença #
Este projeto está sob a licença MIT. Veja o arquivo LICENSE para mais detalhes.