Type-safe PureScript FFI bindings for Redis using ioredis.
This package provides comprehensive bindings to Redis operations including:
- String operations (get, set, setex, etc.)
- Hash operations (hget, hset, hgetall, etc.)
- List operations (lpush, rpush, lpop, rpop, lrange, etc.)
- Set operations (sadd, srem, smembers, etc.)
- Sorted set operations (zadd, zrem, zrange, etc.)
- Pub/Sub support
- Connection management
- TTL and expiry operations
spago install yoga-redisMake sure to also install the ioredis npm package:
npm install ioredisimport 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 clientimport 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 clientimport 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!"All Redis keys are wrapped in a RedisKey newtype for type safety:
newtype RedisKey = RedisKey StringSimilarly, TTL values, passwords, and other configuration values use newtypes:
newtype RedisTTL = RedisTTL Int
newtype RedisPassword = RedisPassword String
newtype RedisHost = RedisHost String
newtype RedisPort = RedisPort IntcreateClient- Create Redis client from URLcreateClientWithConfig- Create client with detailed configurationconnect- Connect to Redisdisconnect- Disconnect from Redisquit- Gracefully quit connectionping- Test connection
get- Get value by keyset- Set key to valuesetex- Set key with expiry timedel- Delete key(s)exists- Check if key existsincr/incrBy- Increment valuedecr/decrBy- Decrement value
hget- Get field from hashhset- Set field in hashhgetall- Get all fields from hashhdel- Delete field from hashhexists- Check if field existshkeys- Get all field nameshlen- Get number of fields
lpush/rpush- Push to listlpop/rpop- Pop from listlrange- Get range of elementsllen- Get list length
sadd- Add members to setsrem- Remove members from setsmembers- Get all memberssismember- Check membershipscard- Get set size
zadd- Add members with scoreszrem- Remove memberszrange- Get range by indexzcard- Get sorted set sizezscore- Get member score
expire- Set key expiryttl- Get time to live
For Om-based applications, see yoga-redis-om which provides Om-wrapped operations and layer management.
- yoga-redis-om - Om-wrapped Redis operations
- ioredis - The underlying Node.js Redis client
MIT