Skip to content

Latest commit

 

History

History

redis_cache

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Redis Cache Example

A page-view counter backed by Redis, using vania_redis with the Vania framework.

Design

The app depends on a small CounterStore contract, with two implementations:

  • RedisCounterStore — real, using Redis().command (INCR / GET / DEL).
  • an in-memory fake in the tests.

A thin PageViews service sits on top, so the counting logic is Redis-free and unit-testable. The controller injects the Redis store; config/app.dart registers RedisServiceProvider.

Endpoints

Method Path Description
POST /api/views/{page} Record a view, return the count
GET /api/views/{page} Read the current count

Running

Needs a reachable Redis (REDIS_HOST / REDIS_PORT, defaults localhost:6379):

docker run -p 6379:6379 redis:7

dart pub get
dart run bin/server.dart
curl -X POST localhost:8000/api/views/home   # {"page":"home","views":1}
curl -X POST localhost:8000/api/views/home   # {"page":"home","views":2}
curl localhost:8000/api/views/home           # {"page":"home","views":2}

Tests

PageViews is tested against the in-memory CounterStore, so no Redis is needed:

dart test