Initial commit
[ric-plt/sdlgo.git] / internal / sdlgoredis / sdlgoredis.go
1 /*
2    Copyright (c) 2019 AT&T Intellectual Property.
3    Copyright (c) 2018-2019 Nokia.
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 */
17
18 package sdlgoredis
19
20 import (
21         "errors"
22         "fmt"
23         "os"
24
25         "github.com/go-redis/redis"
26 )
27
28 type DB struct {
29         client       *redis.Client
30         redisModules bool
31 }
32
33 func Create() *DB {
34         hostname := os.Getenv("DBAAS_SERVICE_HOST")
35         if hostname == "" {
36                 hostname = "localhost"
37         }
38         port := os.Getenv("DBAAS_SERVICE_PORT")
39         if port == "" {
40                 port = "6379"
41         }
42         redisAddress := hostname + ":" + port
43         client := redis.NewClient(&redis.Options{
44                 Addr:     redisAddress,
45                 Password: "", // no password set
46                 DB:       0,  // use default DB
47                 PoolSize: 20,
48         })
49
50         db := DB{
51                 client:       client,
52                 redisModules: true,
53         }
54
55         commands, err := db.client.Command().Result()
56         if err == nil {
57                 redisModuleCommands := []string{"setie", "delie"}
58                 for _, v := range redisModuleCommands {
59                         _, ok := commands[v]
60                         if !ok {
61                                 db.redisModules = false
62                         }
63                 }
64         } else {
65                 fmt.Println(err)
66         }
67         return &db
68 }
69
70 func (db *DB) Close() error {
71         return db.client.Close()
72 }
73
74 func (db *DB) MSet(pairs ...interface{}) error {
75         return db.client.MSet(pairs...).Err()
76 }
77
78 func (db *DB) MGet(keys []string) ([]interface{}, error) {
79         val, err := db.client.MGet(keys...).Result()
80         return val, err
81 }
82
83 func (db *DB) Del(keys []string) error {
84         _, err := db.client.Del(keys...).Result()
85         return err
86 }
87
88 func (db *DB) Keys(pattern string) ([]string, error) {
89         val, err := db.client.Keys(pattern).Result()
90         return val, err
91 }
92
93 func (db *DB) SetIE(key string, oldData, newData interface{}) (bool, error) {
94         if !db.redisModules {
95                 return false, errors.New("Redis deployment not supporting command")
96         }
97
98         result, err := db.client.Do("SETIE", key, newData, oldData).Result()
99         if err != nil {
100                 return false, err
101         }
102         if result == "OK" {
103                 return true, nil
104         } else {
105                 return false, nil
106         }
107 }
108
109 func (db *DB) SetNX(key string, data interface{}) (bool, error) {
110         result, err := db.client.SetNX(key, data, 0).Result()
111         return result, err
112 }
113
114 func (db *DB) DelIE(key string, data interface{}) (bool, error) {
115         if !db.redisModules {
116                 return false, errors.New("Redis deployment not supporting command")
117         }
118         result, err := db.client.Do("DELIE", key, data).Result()
119         if err != nil {
120                 return false, err
121         }
122         if result == "1" {
123                 return true, nil
124         }
125         return false, nil
126 }