Skip to content

go-admin-team/redis-watcher

This branch is 3 commits ahead of, 1 commit behind casbin/redis-watcher:master.

Folders and files

NameName
Last commit message
Last commit date
Jul 4, 2021
Apr 15, 2021
Apr 15, 2021
Jul 6, 2021
Apr 14, 2021
Nov 17, 2022
Nov 2, 2023
Nov 21, 2022
Nov 2, 2023
Nov 2, 2023
Sep 7, 2022

Repository files navigation

Redis Watcher

Go report Coverage Status Go Reference Release

Redis Watcher is a Redis watcher for Casbin.

Installation

go get github.com/casbin/redis-watcher/v2

Simple Example

package main

import (
	"log"

	"github.com/casbin/casbin/v2"
	rediswatcher "github.com/casbin/redis-watcher/v2"
	"github.com/go-redis/redis/v8"
)

func updateCallback(msg string) {
	log.Println(msg)
}

func main() {
	// Initialize the watcher.
	// Use the Redis host as parameter.
	w, _ := rediswatcher.NewWatcher("localhost:6379", rediswatcher.WatcherOptions{
		Options: redis.Options{
			Network:  "tcp",
			Password: "",
		},
		Channel:    "/casbin",
		// Only exists in test, generally be true
		IgnoreSelf: false,
	})

	// Or initialize the watcher in redis cluster.
	// w, _ := rediswatcher.NewWatcherWithCluster("localhost:6379,localhost:6379,localhost:6379", rediswatcher.WatcherOptions{
	// 	ClusterOptions: redis.ClusterOptions{
	// 		Password: "",
	// 	},
	// 	Channel: "/casbin",
	// 	IgnoreSelf: false,
	// })

	// Initialize the enforcer.
	e, _ := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")

	// Set the watcher for the enforcer.
	_ = e.SetWatcher(w)

	// Set callback to local example
	_ = w.SetUpdateCallback(updateCallback)
	
	// Or use the default callback
	// _ = w.SetUpdateCallback(rediswatcher.DefaultUpdateCallback(e))

	// Update the policy to test the effect.
	// You should see "[casbin rules updated]" in the log.
	_ = e.SavePolicy()
	// Only exists in test
	fmt.Scanln()
}

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%