hapredis

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 5 Imported by: 0

README

hapredis

A simple adapter to use go-redis as storage backend for brutella/hap.

Usage

Download the library within the context of your project:

go get github.com/brutella/hap
go get github.com/go-redis/redis/v8
go get github.com/mologie/hapredis

Integrate it by creating a Redis client and wrapping into a Store interface.

It is important to give your client a unique prefix to allow multiple different instances to connect to use a single Redis database.

package main

import (
	"context"
	"github.com/brutella/hap"
	"github.com/brutella/hap/accessory"
	"github.com/go-redis/redis/v8"
	"github.com/mologie/hapredis"
	"log"
)

func main() {
	ctx := context.Background()

	// create storage backend using this library
	redisClient := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
	store := hapredis.NewStore(ctx, redisClient, "unique-instance-prefix:")

	// create accessory and its server
	acc := accessory.NewSwitch(accessory.Info{Name: "Lamp"})
	server, err := hap.NewServer(store, acc.A)
	if err != nil {
		// Redis errors can get you here, e.g. when the server cannot load its
		// UUID due to connection or permission problems.
		log.Panic(err)
	}

	// run accessory server forever
	server.ListenAndServe(ctx)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a Redis storage adapter for hap.Server.

func NewStore

func NewStore(ctx context.Context, client *redis.Client, prefix string) *Store

NewStore creates a Redis adapter that conforms to hap.Store.

Set its context to the server's context to cancel stuck Redis operations when the server is shut down, or to context.Background() when no ordinary shutdown is implemented.

The client instance must be created through go-redis.

The prefix must be unique for your hap server instance. It is convention to use a colon symbol as separator in Redis keys. The prefix length is irrelevant with the amount of data stored by hap. An example for a short and nice prefix is "fooapp:barhost:", where barhost is a hostname.

func (Store) Delete

func (s Store) Delete(key string) error

func (Store) Get

func (s Store) Get(key string) ([]byte, error)

func (Store) KeysWithSuffix

func (s Store) KeysWithSuffix(suffix string) ([]string, error)

func (Store) Set

func (s Store) Set(key string, value []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL