-
Notifications
You must be signed in to change notification settings - Fork 0
/
nscache.go
48 lines (40 loc) · 938 Bytes
/
nscache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package nscache
import (
"errors"
"fmt"
"net/url"
"strings"
"time"
)
// ErrNil get nil data
var ErrNil = errors.New("nscache: nil")
// NSCache the core interface of the cache
type NSCache interface {
NSCacheExt
// Get get cache data by key
Get(k string, v any) error
// Set set new cache data
Set(k string, v any, expiration time.Duration) error
// Remove remove the specified key
Remove(k string) error
// Close close the cache component
Close() error
}
// NewCache get an instance of NSCache by connection string
func NewCache(conn string) (NSCache, error) {
u, err := url.Parse(conn)
if err != nil {
return nil, err
}
driverName := strings.ToLower(u.Scheme)
if len(driverName) == 0 {
return nil, errInvalidCacheDriverName
}
mu.RLock()
factory := drivers[driverName]
mu.RUnlock()
if factory == nil {
return nil, fmt.Errorf("%w => %s", errUnsupportedCacheDriver, driverName)
}
return factory(u)
}