From 515156eddb0c6c6b6665184aaaf4f25c5baddce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=96zelo=C4=9Flu?= Date: Sat, 16 Oct 2021 22:05:19 +0300 Subject: [PATCH] Add doc.go --- doc.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 doc.go diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..7b7d3ec --- /dev/null +++ b/doc.go @@ -0,0 +1,68 @@ +/* +Package kvs is the in-memory key-value storage for Go. It has 2 basic features +which are Get and Set. Basically, Set adds key-value pair in map and Get returns +the value of the given key. If there is no such a key in map, it returns empty +string. + +Create Kvs object once before calling Get and Set methods: + + db, _ := kvs.Create("", "users", 1*time.Minute) + db.Set("foo", "bar") + val := db.Get("foo") + +Before diving into the package, you need to know that kvs package can be used +in 2 different ways. As a first option, it can be used as a package that can +be imported at the beginning of the code. Then, you can call the functions as +above. As another way, you can create an HTTP server and call endpoints as +defined in server.go. You can send requests to server as regular API call. + +You can call endpoints like this: + + http://localhost:1234/set --> POST + Body: + { + "data": [ + { + "key": "joe", + "value": "13" + } + ] + } + + http://localhost:1234/get/joe --> GET + + http://localhost:1234/save --> PUT + +Default port is assigned as :1234 for this server. Actually, it is not used for +package usage, only is used for server usage. + +While creating Kvs object, you need to specify database name. It is obligatory +to pass it to Create function. If it is not specified, Create function returns +error. + +Here is the sample usage: + + // Db name is "users" + db, err := kvs.Create(":1234", "users", 1*time.Minute) + + // Returns error + db, err := kvs.Create(":1234", "", 1*time.Minute) + +For the server usage, after creating Kvs object you need to call Open method to +start server. Here is the usage: + + func main() { + db, err := kvs.Create(":1234", "users", 1*time.Minute) + if err != nil { + log.Fatalf(err.Error()) + } + + // Starts server on localhost:1234 + db.Open() + } + +In Create function, there is third parameter named as duration. It provides +saving data periodically in given interval time. For example, if you specify +2*time.Minute as parameter, the data in map would be saved every 2 minutes. +*/ +package kvs