zergon321

Golang Redis example

Jan 25th, 2021
895
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package shared
  2.  
  3. import (
  4.     "biocad-opcua/data"
  5.     "fmt"
  6.     "log"
  7.     "strconv"
  8.  
  9.     "github.com/go-redis/redis"
  10. )
  11.  
  12. // Cache provides an interface to write and read alert thresholds for the parameters.
  13. type Cache struct {
  14.     client   *redis.Client
  15.     endpoint string
  16.     logger   *log.Logger
  17. }
  18.  
  19. // Connect establishes a new connection with the cache server.
  20. func (cache *Cache) Connect() {
  21.     cache.client = redis.NewClient(&redis.Options{
  22.         Addr:     cache.endpoint,
  23.         Password: "", // No password.
  24.         DB:       0,  // Default database.
  25.     })
  26. }
  27.  
  28. // CloseConnection gracefully closes the connection to the cache.
  29. func (cache *Cache) CloseConnection() {
  30.     cache.client.Close()
  31. }
  32.  
  33. // CheckParameterBoundsExist checks if the parameter bounds exist in the cache.
  34. func (cache *Cache) CheckParameterBoundsExist(parameter string) (bool, error) {
  35.     result, err := cache.client.Exists(parameter).Result()
  36.     cache.handleCheckParameterBoundsExistError(err)
  37.  
  38.     if err != nil {
  39.         return false, err
  40.     }
  41.  
  42.     if result > 0 {
  43.         return true, nil
  44.     }
  45.  
  46.     return false, nil
  47. }
  48.  
  49. // CheckParameterExists checks if the parameter exists in the cache.
  50. func (cache *Cache) CheckParameterExists(parameter string) (bool, error) {
  51.     exists, err := cache.client.SIsMember("parameters", parameter).Result()
  52.     cache.handleCheckParameterExistsError(err)
  53.  
  54.     if err != nil {
  55.         return false, err
  56.     }
  57.  
  58.     if exists {
  59.         return true, nil
  60.     }
  61.  
  62.     return false, nil
  63. }
  64.  
  65. // AddParameters adds new parameter names to the cache.
  66. func (cache *Cache) AddParameters(parameters ...string) error {
  67.     values := make([]interface{}, len(parameters))
  68.  
  69.     for i := range parameters {
  70.         values[i] = parameters[i]
  71.     }
  72.  
  73.     // If the parameter doesn't exist, add it to the set.
  74.     err := cache.client.SAdd("parameters", values...).Err()
  75.     cache.handleAddParameterError(err)
  76.  
  77.     return err
  78. }
  79.  
  80. // GetAllParameters returns a list of the all parameters from the cache.
  81. func (cache *Cache) GetAllParameters() ([]string, error) {
  82.     parameters, err := cache.client.SMembers("parameters").Result()
  83.     cache.handleGetAllParametersError(err)
  84.  
  85.     if err != nil {
  86.         return nil, err
  87.     }
  88.  
  89.     return parameters, nil
  90. }
  91.  
  92. // GetParameterBounds returns alert thresholds for the parameter.
  93. func (cache *Cache) GetParameterBounds(parameter string) (data.Bounds, error) {
  94.     bounds := data.Bounds{}
  95.     fields, err := cache.client.HGetAll(parameter).Result()
  96.     cache.handleGetParameterBoundsError(err)
  97.  
  98.     if err != nil {
  99.         return data.Bounds{}, err
  100.     }
  101.  
  102.     var (
  103.         lowerBound, upperBound string
  104.         ok                     bool
  105.     )
  106.  
  107.     // Get the lower bound.
  108.     if lowerBound, ok = fields["lower_bound"]; !ok {
  109.         message := "Parameter 'lower_bound' is missing in the cache"
  110.  
  111.         cache.logger.Println(message)
  112.  
  113.         return data.Bounds{}, fmt.Errorf(message)
  114.     }
  115.  
  116.     temp, err := strconv.ParseFloat(lowerBound, 64)
  117.     cache.handleParameterValueCastError(err)
  118.  
  119.     if err != nil {
  120.         return data.Bounds{}, err
  121.     }
  122.  
  123.     bounds.LowerBound = temp
  124.  
  125.     // Get the upper bound.
  126.     if upperBound, ok = fields["upper_bound"]; !ok {
  127.         message := "Parameter 'upper_bound' is missing in the cache"
  128.  
  129.         cache.logger.Println(message)
  130.  
  131.         return data.Bounds{}, fmt.Errorf(message)
  132.     }
  133.  
  134.     temp, err = strconv.ParseFloat(upperBound, 64)
  135.     cache.handleParameterValueCastError(err)
  136.  
  137.     if err != nil {
  138.         return data.Bounds{}, err
  139.     }
  140.  
  141.     bounds.UpperBound = temp
  142.  
  143.     return bounds, nil
  144. }
  145.  
  146. // SetParameterBounds assigns new alert thresholds to the parameter.
  147. func (cache *Cache) SetParameterBounds(parameter string, bounds data.Bounds) error {
  148.     fields := map[string]interface{}{
  149.         "lower_bound": bounds.LowerBound,
  150.         "upper_bound": bounds.UpperBound,
  151.     }
  152.  
  153.     // Change the bounds for the parameter.
  154.     err := cache.client.HMSet(parameter, fields).Err()
  155.     cache.handleSetParameterBoundsError(err)
  156.  
  157.     if err != nil {
  158.         return err
  159.     }
  160.  
  161.     // If the parameter doesn't exist, add it to the set.
  162.     return cache.AddParameters(parameter)
  163. }
  164.  
  165. // NewCache creates a new cache client.
  166. func NewCache(endpoint string, logger *log.Logger) *Cache {
  167.     return &Cache{
  168.         endpoint: endpoint,
  169.         logger:   logger,
  170.     }
  171. }
  172.  
  173. func (cache *Cache) handleGetParameterBoundsError(err error) {
  174.     if err != nil {
  175.         cache.logger.Println("Couldn't get bounds for the parameter:", err)
  176.     }
  177. }
  178.  
  179. func (cache *Cache) handleParameterValueCastError(err error) {
  180.     if err != nil {
  181.         cache.logger.Println("The parameter value is incorrect:", err)
  182.     }
  183. }
  184.  
  185. func (cache *Cache) handleSetParameterBoundsError(err error) {
  186.     if err != nil {
  187.         cache.logger.Println("Couldn't set parameter bounds in the cache:", err)
  188.     }
  189. }
  190.  
  191. func (cache *Cache) handleGetAllParametersError(err error) {
  192.     if err != nil {
  193.         cache.logger.Println("Couldn't obtain a list of parameters from the cache", err)
  194.     }
  195. }
  196.  
  197. func (cache *Cache) handleAddParameterError(err error) {
  198.     if err != nil {
  199.         cache.logger.Println("Couldn't add the parameter to the cache", err)
  200.     }
  201. }
  202.  
  203. func (cache *Cache) handleCheckParameterExistsError(err error) {
  204.     if err != nil {
  205.         cache.logger.Println("Couldn't check if the parameter exists in the cache", err)
  206.     }
  207. }
  208.  
  209. func (cache *Cache) handleCheckParameterBoundsExistError(err error) {
  210.     if err != nil {
  211.         cache.logger.Println("Couldn't check if the parameter bounds exist in the cache", err)
  212.     }
  213. }
  214.  
  215. func (cache *Cache) handleSnapshotSaveError(err error) {
  216.     if err != nil {
  217.         cache.logger.Println("Couldn't save the cache snapshot to the disk", err)
  218.     }
  219. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×