memory-db-redis.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Author : Eric Kim
  2. // Build Date : 23 Jul 2018 Last Update 02 Aug 2018
  3. // End-Agent for Passcon Multi OS go binding with Windows, MacOS, iOS, and Android
  4. // All rights are reserved.
  5. package abango
  6. import (
  7. "context"
  8. "time"
  9. "github.com/dabory/abango-rest/etc"
  10. e "github.com/dabory/abango-rest/etc"
  11. "github.com/go-redis/redis/v8"
  12. // "github.com/tidwall/buntdb"
  13. )
  14. var (
  15. // MDB *buntdb.DB
  16. // QDB *buntdb.DB
  17. RedisCtx = context.Background()
  18. MDB *redis.Client
  19. //QDB은 Redis를 도입하면서 통합함.
  20. QDBOn bool // QDb에서 쿼리 가져옴
  21. )
  22. func MdbView(key string) (retval string, reterr error) {
  23. value, err := MDB.Get(RedisCtx, key).Result()
  24. if err == redis.Nil {
  25. reterr = e.LogErr("ASDFQWERCAA", "MDB.View Not Fount in Key: "+key, err)
  26. } else if err != nil {
  27. reterr = e.LogErr("ASDFQWERA", "MDB.View Error reading data: "+key, err)
  28. }
  29. return value, reterr
  30. }
  31. func MdbUpdate(key string, value string) (reterr error) {
  32. REDIS_EXTIME := 12 * time.Hour
  33. err := MDB.Set(RedisCtx, key, value, REDIS_EXTIME).Err()
  34. if err != nil {
  35. reterr = e.MyErr("QWVGAVAEFV-MDB.Update Error in Key: "+key+" Value: "+value, err, false)
  36. }
  37. return nil
  38. }
  39. func MdbDelete(key string, value string) (reterr error) {
  40. _, err := MDB.Del(RedisCtx, "mykey").Result()
  41. if err != nil {
  42. reterr = e.MyErr("LJOOHOHIG-MDB.Delete Error in Key: "+key+" Value: "+value, err, false)
  43. }
  44. return
  45. }
  46. // func QdbView(key string) (retval string, reterr error) {
  47. // QDB.View(func(tx *buntdb.Tx) error {
  48. // if value, err := tx.Get(key); err == nil {
  49. // retval = value
  50. // reterr = nil
  51. // } else {
  52. // retval = ""
  53. // reterr = errors.New("QDB.View Not Found in Key: " + key)
  54. // }
  55. // return nil
  56. // })
  57. // return retval, reterr
  58. // }
  59. // func QdbUpdate(key string, value string) (reterr error) {
  60. // QDB.Update(func(tx *buntdb.Tx) error {
  61. // _, _, err := tx.Set(key, value, nil)
  62. // if err != nil {
  63. // reterr = e.MyErr("TKBKUYIH-QDB.Update Error in Key: "+key+" Value: "+value, err, false)
  64. // }
  65. // return nil
  66. // })
  67. // return nil
  68. // }
  69. func GetQryStr(filename string) (string, error) {
  70. var str string
  71. var err error
  72. if QDBOn {
  73. if str, err = MdbView(filename); err == nil {
  74. // etc.OkLog("Qry from Memory!!")
  75. return str, nil
  76. } else {
  77. if str, err = e.FileToStrSkip(filename); err == nil {
  78. if err := MdbUpdate(filename, str); err != nil {
  79. return "", etc.LogErr("OIUJLJOUJLH", "QdbUpdate Failed ", err)
  80. }
  81. // etc.OkLog("Qry from File!!")
  82. return str, nil
  83. } else {
  84. return "", err
  85. }
  86. }
  87. } else {
  88. if str, err = e.FileToStrSkip(filename); err == nil {
  89. // etc.OkLog("QRY FILE")
  90. return str, nil
  91. } else {
  92. return "", etc.LogErr("PKOJHKJUY", " File", err)
  93. }
  94. }
  95. }