Skip to content

Commit

Permalink
README: fix example code
Browse files Browse the repository at this point in the history
- add "header" sections (package and import)
- add missing word "struct" in the User type declaration
- replace "new" with composite literal to get rid of unnecessary
assignments
- add missing returned error receiver to MCache.Set and MCache.SetPointer
- fix MCache name where methods Get and GetPointer are called
- fix typo objUser,Bio -> objUser.Bio
- last log.Println should be Printf in order to replace "%s" with the
key value
  • Loading branch information
olshevskiy87 committed Apr 16, 2018
1 parent 5e0f002 commit 342fcf5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
*.sw[pno]
.bashrc
/src
/src
89 changes: 55 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,74 +21,95 @@ with expiration times, it doesn't need to serialize, and quick removal of expire
**Example a Pointer value (vary fast method)**

```go
package main

import (
"fmt"
"log"
"time"

mcache "github.com/OrlovEvgeny/go-mcache"
)

var MCache *mcache.CacheDriver

type User {
type User struct {
Name string
Age uint
Bio string
Age uint
Bio string
}

func main() {
MCache = mcache.StartInstance()

key := "key1"

user := new(User)
user.Name = "John"
user.Age = 20
user.Bio = "gopher"

user := &User{
Name: "John",
Age: 20,
Bio: "gopher",
}
//args - key, &value, ttl
MCache.SetPointer(key, user, time.Minute*20)
err := MCache.SetPointer(key, user, time.Minute*20)
if err != nil {
log.Println("MCACHE SET ERROR: ", err)
log.Println("MCACHE SET ERROR:", err)
}
if pointer, ok := mcache.GetPointer(key); ok {

if pointer, ok := MCache.GetPointer(key); ok {
if objUser, ok := pointer.(*User); ok {
fmt.Printf("User name: %s, Age: %d, Bio: %s\n", objUser.Name, objUser.Age, objUser,Bio)
fmt.Printf("User name: %s, Age: %d, Bio: %s\n", objUser.Name, objUser.Age, objUser.Bio)
}
} else {
log.Println("Cache by key: %s not found", key)
log.Printf("Cache by key: %s not found\n", key)
}
}
```



**Example serialize and deserialize value**

```go
package main

import (
"fmt"
"log"
"time"

mcache "github.com/OrlovEvgeny/go-mcache"
)

var MCache *mcache.CacheDriver

type User {
type User struct {
Name string
Age uint
Bio string
Age uint
Bio string
}

func main() {
MCache = mcache.StartInstance()

key := "key1"
userSet := new(User)
userSet.Name = "John"
userSet.Age = 20
userSet.Bio = "gopher"


userSet := &User{
Name: "John",
Age: 20,
Bio: "gopher",
}
//args - key, &value, ttl
MCache.Set(key, userSet, time.Minute*20)
err := MCache.Set(key, userSet, time.Minute*20)
if err != nil {
log.Println("MCACHE SET ERROR: ", err)
log.Println("MCACHE SET ERROR:", err)
}



var userGet User
if ok := mcache.Get(key, &userGet); ok {
fmt.Printf("User name: %s, Age: %d, Bio: %s\n", userGet.Name, userGet.Age, userGet,Bio)
} else {
log.Println("Cache by key: %s not found", key)
}
if ok := MCache.Get(key, &userGet); ok {
fmt.Printf("User name: %s, Age: %d, Bio: %s\n", userGet.Name, userGet.Age, userGet.Bio)
} else {
log.Printf("Cache by key: %s not found\n", key)
}
}
```

Expand Down

0 comments on commit 342fcf5

Please sign in to comment.