Skip to content

Commit 0cd40e5

Browse files
authored
feat: add NewAdapterWithPool() (#37)
* feat: add pool adapter * fix: add missing key * docs: add pool adapter example
1 parent 5c97830 commit 0cd40e5

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ func main() {
2626
// Use the following if you use Redis with a specific user
2727
// a, err := redisadapter.NewAdapterWithUser("tcp", "127.0.0.1:6379", "username", "password")
2828

29+
// Use the following if you use Redis connections pool
30+
// pool := &redis.Pool{}
31+
// a, err := redisadapter.NewAdapterWithPool(pool)
32+
2933
e := casbin.NewEnforcer("examples/rbac_model.conf", a)
3034

3135
// Load the policy from DB.

adapter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ func NewAdapterWithKey(network string, address string, key string) (*Adapter, er
9292
return newAdapter(network, address, key, "", "")
9393
}
9494

95+
// NewAdapterWithPool is the constructor for Adapter.
96+
func NewAdapterWithPool(pool *redis.Pool) (*Adapter, error) {
97+
a := &Adapter{}
98+
a.key = "casbin_rules"
99+
a.conn = pool.Get()
100+
101+
// Call the destructor when the object is released.
102+
runtime.SetFinalizer(a, finalizer)
103+
104+
return a, nil
105+
}
106+
95107
type Option func(*Adapter)
96108

97109
func NewAdpaterWithOption(options ...Option) (*Adapter, error) {

adapter_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/casbin/casbin/v2"
2323
"github.com/casbin/casbin/v2/util"
24+
"github.com/gomodule/redigo/redis"
2425
)
2526

2627
func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
@@ -356,7 +357,7 @@ func TestAdapters(t *testing.T) {
356357
a, _ := NewAdapter("tcp", "127.0.0.1:6379")
357358

358359
// Use the following if Redis has password like "123"
359-
//a, err := NewAdapterWithPassword("tcp", "127.0.0.1:6379", "123")
360+
// a, err := NewAdapterWithPassword("tcp", "127.0.0.1:6379", "123")
360361

361362
// Use the following if you use Redis with a account
362363
// a, err := NewAdapterWithUser("tcp", "127.0.0.1:6379", "testaccount", "userpass")
@@ -369,3 +370,22 @@ func TestAdapters(t *testing.T) {
369370
testUpdatePolicies(t, a)
370371
testUpdateFilteredPolicies(t, a)
371372
}
373+
374+
func TestPoolAdapters(t *testing.T) {
375+
a, err := NewAdapterWithPool(&redis.Pool{
376+
Dial: func() (redis.Conn, error) {
377+
return redis.Dial("tcp", "127.0.0.1:6379")
378+
},
379+
})
380+
if err != nil {
381+
t.Fatal(err)
382+
}
383+
384+
testSaveLoad(t, a)
385+
testAutoSave(t, a)
386+
testFilteredPolicy(t, a)
387+
testAddPolicies(t, a)
388+
testRemovePolicies(t, a)
389+
testUpdatePolicies(t, a)
390+
testUpdateFilteredPolicies(t, a)
391+
}

0 commit comments

Comments
 (0)