sosyz commented on code in PR #1265:
URL: https://github.com/apache/answer/pull/1265#discussion_r1976238619


##########
plugin/kv_storage.go:
##########
@@ -0,0 +1,344 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package plugin
+
+import (
+       "context"
+       "encoding/json"
+       "fmt"
+       "math/rand"
+       "time"
+
+       "github.com/apache/answer/internal/entity"
+       "github.com/segmentfault/pacman/log"
+       "xorm.io/builder"
+       "xorm.io/xorm"
+)
+
+// define error
+var (
+       ErrKVKeyNotFound        = fmt.Errorf("key not found in KV storage")
+       ErrKVGroupEmpty         = fmt.Errorf("group name is empty")
+       ErrKVKeyEmpty           = fmt.Errorf("key name is empty")
+       ErrKVKeyAndGroupEmpty   = fmt.Errorf("both key and group are empty")
+       ErrKVTransactionFailed  = fmt.Errorf("KV storage transaction failed")
+       ErrKVDataNotInitialized = fmt.Errorf("KV storage data not initialized")
+       ErrKVDBNotInitialized   = fmt.Errorf("KV storage database connection 
not initialized")
+)
+
+type KVOperator struct {
+       data           *Data
+       session        *xorm.Session
+       pluginSlugName string
+}
+
+func (kv *KVOperator) checkDB() error {
+       if kv.data == nil {
+               return ErrKVDataNotInitialized
+       }
+       if kv.data.DB == nil {
+               return ErrKVDBNotInitialized
+       }
+       return nil
+}
+
+func (kv *KVOperator) getSession(ctx context.Context) (session *xorm.Session, 
close func()) {
+       if kv.session != nil {
+               session = kv.session
+       } else {
+               session = kv.data.DB.NewSession().Context(ctx)
+               close = func() {
+                       if session != nil {
+                               session.Close()
+                       }
+               }
+       }
+       return
+}
+
+func (kv *KVOperator) getCacheTTL() time.Duration {
+       return 30*time.Minute + time.Duration(rand.Intn(300))*time.Second

Review Comment:
   @LinkinStars 
   I added some code to allow developers to configure the cache. It now 
defaults to 30 minutes, and if set to `-1`, the cache will be disabled.
   ```go
   type KVOperator struct {
        data           *Data
        session        *xorm.Session
        pluginSlugName string
        cacheTTL       time.Duration
   }
   
   // KVStorageOption is the option for the KV storage
   type KVStorageOption func(*KVOperator)
   
   // WithCacheTTL is the option to set the cache TTL
   func WithCacheTTL(ttl time.Duration) KVStorageOption {
        return func(kv *KVOperator) {
                kv.cacheTTL = ttl
        }
   }
   
   // Option is used to set the options for the KV storage
   func (kv *KVOperator) Option(opts ...KVStorageOption) {
        for _, opt := range opts {
                opt(kv)
        }
   }
   ```
   What do you think? I believe it should make the caching mechanism more 
flexible.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@answer.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to