Om-wrapped Redis operations for PureScript using the Yoga Om effect system.
This package provides Om-wrapped versions of Redis operations from yoga-redis, allowing you to use Redis within the Om effect system without manually threading the Redis client through your code.
spago install yoga-redis-om yoga-om-core yoga-om-layerimport Yoga.Redis.Om as Redis
import Yoga.Redis.OmLayer as RedisLayer
import Yoga.Om (runOm)
main = launchAff_ do
let config =
{ host: "localhost"
, port: 6379
, password: Nothing
, db: Nothing
, keyPrefix: Nothing
, connectTimeout: Nothing
}
runOm (RedisLayer.live config) do
-- All operations use the implicit Redis client from the environment
Redis.set "mykey" "myvalue"
value <- Redis.get "mykey"
log $ "Retrieved: " <> show valueimport Yoga.Om ((:>), type (:>))
import Yoga.Redis.Om as Redis
import Yoga.Redis.OmLayer (RedisEnv)
-- Your application needs Redis
type AppEnv = RedisEnv :> ()
myApp :: forall r. Has RedisEnv r => Om r Unit
myApp = do
Redis.set "counter" "0"
Redis.incr "counter"
count <- Redis.get "counter"
log $ "Count: " <> show count
main = launchAff_ do
runOm (RedisLayer.live config) myAppimport Yoga.Redis.Om as Redis
saveUser :: forall r. Has RedisEnv r => Om r Unit
saveUser = do
Redis.hset "user:1" "name" "Alice"
Redis.hset "user:1" "email" "alice@example.com"
Redis.hset "user:1" "age" "30"
user <- Redis.hgetall "user:1"
log $ "User data: " <> show userimport Yoga.Redis.Om as Redis
manageQueue :: forall r. Has RedisEnv r => Om r Unit
manageQueue = do
-- Add items to queue
Redis.lpush "tasks" ["task1", "task2", "task3"]
-- Get queue length
length <- Redis.llen "tasks"
log $ "Queue length: " <> show length
-- Process items
task <- Redis.rpop "tasks"
case task of
Just t -> log $ "Processing: " <> t
Nothing -> log "Queue is empty"All operations from yoga-redis are available with Om wrappers. The key difference is that you don't need to pass the Redis client explicitly - it's provided by the Om environment.
get- Get value by keyset- Set key to valuesetex- Set key with expirydel- Delete key(s)exists- Check if key existsincr/incrBy- Incrementdecr/decrBy- Decrement
hget- Get field from hashhset- Set field in hashhgetall- Get all fieldshdel- Delete fieldhexists- Check field existshkeys- Get all keyshlen- Get number of fields
lpush/rpush- Push to listlpop/rpop- Pop from listlrange- Get rangellen- Get length
sadd- Add memberssrem- Remove memberssmembers- Get all memberssismember- Check membershipscard- Get size
zadd- Add with scorezrem- Remove memberszrange- Get rangezcard- Get sizezscore- Get score
ping- Test connectionpublish- Publish to channelexpire- Set expiryttl- Get time to live
import Yoga.Redis.OmLayer as RedisLayer
-- Simple configuration
let config = { url: "redis://localhost:6379" }
let layer = RedisLayer.live config
-- Detailed configuration
let config =
{ host: "localhost"
, port: 6379
, password: Just "secret"
, db: Just 0
, keyPrefix: Just "myapp:"
, connectTimeout: Just (Milliseconds 10000.0)
}
let layer = RedisLayer.live configThe Om system ensures at compile time that your Redis layer is provided:
-- This will compile
runOm (RedisLayer.live config) Redis.ping
-- This will NOT compile (missing Redis layer)
runOm emptyLayer Redis.ping- No Client Threading - Redis client is implicit in the environment
- Composable - Easily combine with other Om layers
- Type-Safe - Compiler ensures required layers are provided
- Testable - Easy to mock Redis layer for testing
- Clean Code - Less boilerplate, more readable
You can create mock Redis layers for testing:
import Yoga.Redis.OmLayer as RedisLayer
mockRedisLayer :: Layer (RedisEnv :> ()) (RedisEnv :> ())
mockRedisLayer = -- implement mock behavior
spec = describe "MyApp" do
it "works with Redis" do
result <- runOm mockRedisLayer myApp
result `shouldEqual` expected- yoga-redis - Raw Redis bindings
- yoga-om-core - Om effect system core
- yoga-om-layer - Om layer management
MIT