git.iko.soy/rowtype-yoga/purescript-yoga-redis
on main help sign in

Redis client bindings for PureScript - part of the Yoga backend stack

git clone ssh://git.iko.soy/rowtype-yoga/purescript-yoga-redis

yoga-redis

Type-safe PureScript FFI bindings for Redis using ioredis.

Overview

This package provides comprehensive bindings to Redis operations including:

Installation

spago install yoga-redis

Make sure to also install the ioredis npm package:

npm install ioredis

Usage

Basic Operations

import Yoga.Redis as Redis
import Effect.Aff (launchAff_)

main = launchAff_ do
  -- Create and connect
  client <- Redis.createClient { url: "redis://localhost:6379" }
  Redis.connect client
  
  -- String operations
  Redis.set client (Redis.RedisKey "mykey") "myvalue"
  value <- Redis.get client (Redis.RedisKey "mykey")
  
  -- With expiry
  Redis.setex client (Redis.RedisKey "tempkey") (Redis.RedisTTL 60) "tempvalue"
  
  -- Hash operations
  Redis.hset client (Redis.RedisKey "user:1") "name" "John"
  Redis.hset client (Redis.RedisKey "user:1") "email" "john@example.com"
  user <- Redis.hgetall client (Redis.RedisKey "user:1")
  
  -- List operations
  Redis.lpush client (Redis.RedisKey "mylist") ["item1", "item2"]
  items <- Redis.lrange client (Redis.RedisKey "mylist") 0 (-1)
  
  -- Cleanup
  Redis.disconnect client

Configuration

import Yoga.Redis as Redis

config :: Redis.RedisConfig
config = 
  { host: Redis.RedisHost "localhost"
  , port: Redis.RedisPort 6379
  , password: Just (Redis.RedisPassword "mypassword")
  , db: Just (Redis.RedisDatabase 0)
  , keyPrefix: Just (Redis.RedisKeyPrefix "myapp:")
  , connectTimeout: Just (Redis.ConnectTimeout (Milliseconds 10000.0))
  }

main = launchAff_ do
  client <- Redis.createClientWithConfig config
  -- ... use client

Pub/Sub

import Yoga.Redis.PubSub as RedisPubSub

main = launchAff_ do
  -- Create separate clients for pub and sub
  pubClient <- Redis.createClient { url: "redis://localhost:6379" }
  subClient <- Redis.createClient { url: "redis://localhost:6379" }
  
  -- Subscribe to channel
  RedisPubSub.subscribe subClient "notifications" \message -> do
    log $ "Received: " <> message
  
  -- Publish message
  RedisPubSub.publish pubClient "notifications" "Hello, World!"

Type Safety

All Redis keys are wrapped in a RedisKey newtype for type safety:

newtype RedisKey = RedisKey String

Similarly, TTL values, passwords, and other configuration values use newtypes:

newtype RedisTTL = RedisTTL Int
newtype RedisPassword = RedisPassword String
newtype RedisHost = RedisHost String
newtype RedisPort = RedisPort Int

API Reference

Connection Management

String Operations

Hash Operations

List Operations

Set Operations

Sorted Set Operations

TTL Operations

Integration with Om

For Om-based applications, see yoga-redis-om which provides Om-wrapped operations and layer management.

License

MIT