Skip to content

Commit 124382f

Browse files
committed
Update and add more docs.
1 parent 4107789 commit 124382f

File tree

9 files changed

+333
-202
lines changed

9 files changed

+333
-202
lines changed

README.md

Lines changed: 46 additions & 202 deletions
Original file line numberDiff line numberDiff line change
@@ -2,265 +2,109 @@
22

33
[![Build Status](https://img.shields.io/travis/faabiosr/cachego/master.svg?style=flat-square)](https://travis-ci.org/faabiosr/cachego)
44
[![Codecov branch](https://img.shields.io/codecov/c/github/faabiosr/cachego/master.svg?style=flat-square)](https://codecov.io/gh/faabiosr/cachego)
5-
[![GoDoc](https://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](https://godoc.org/github.com/faabiosr/cachego)
5+
[![GoDoc](https://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](https://pkg.go.dev/github.com/faabiosr/cachego)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/faabiosr/cachego?style=flat-square)](https://goreportcard.com/report/github.com/faabiosr/cachego)
77
[![License](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://github.com/faabiosr/cachego/blob/master/LICENSE)
88

99
Simple interface for caching
1010

1111
## Installation
1212

13-
Cachego requires Go 1.9 or later.
13+
Cachego requires Go 1.13 or later.
1414

1515
```
1616
go get github.com/faabiosr/cachego
1717
```
1818

19-
If you want to get an specific version, please use the example below:
20-
21-
```
22-
go get gopkg.in/faabiosr/cachego.v0
23-
```
24-
25-
## Usage Examples
26-
27-
### Memcached
28-
29-
```go
30-
package main
31-
32-
import (
33-
"github.com/faabiosr/cachego"
34-
"github.com/bradfitz/gomemcache/memcache"
35-
)
36-
37-
var cache cachego.Cache
38-
39-
func init() {
40-
cache = cachego.NewMemcached(memcached.New("localhost:11211"))
41-
}
42-
```
43-
44-
### Redis
45-
46-
```go
47-
package main
48-
49-
import (
50-
"github.com/faabiosr/cachego"
51-
"gopkg.in/redis.v4"
52-
)
53-
54-
var cache cachego.Cache
55-
56-
func init() {
57-
cache = cachego.NewRedis(
58-
redis.NewClient(&redis.Options{
59-
Addr: ":6379",
60-
}),
61-
)
62-
}
63-
```
64-
65-
### File
66-
67-
```go
68-
package main
69-
70-
import (
71-
"github.com/faabiosr/cachego"
72-
)
73-
74-
var cache cachego.Cache
75-
76-
func init() {
77-
cache = cachego.NewFile(
78-
"/cache-dir/",
79-
)
80-
}
81-
```
82-
83-
### Map
84-
85-
```go
86-
package main
87-
88-
import (
89-
"github.com/faabiosr/cachego"
90-
)
91-
92-
var cache cachego.Cache
93-
94-
func init() {
95-
cache = NewMap()
96-
}
97-
```
98-
99-
### MongoDB
19+
## Usage
10020

10121
```go
10222
package main
10323

10424
import (
105-
"github.com/faabiosr/cachego"
106-
"gopkg.in/mgo.v2"
107-
)
108-
109-
var cache cachego.Cache
110-
111-
func init() {
112-
session, _ := mgo.Dial(address)
113-
114-
cache = cachego.NewMongo(
115-
session.DB("cache").C("cache"),
116-
)
117-
}
118-
```
119-
120-
### Sqlite3
25+
"log"
26+
"time"
12127

122-
```go
123-
package main
124-
125-
import (
126-
"database/sql"
127-
_ "github.com/mattn/go-sqlite3"
28+
"github.com/faabiosr/cachego/sync"
12829
)
12930

130-
var cache cachego.Cache
131-
132-
func init() {
133-
db, _ := sql.Open("sqlite3", "./cache.db")
134-
135-
cache, _ = NewSqlite3(db, "cache")
136-
}
137-
```
31+
func main() {
32+
cache := sync.New()
13833

139-
### SyncMap
34+
if err := cache.Save("user_id", "1", 10*time.Second); err != nil {
35+
log.Fatal(err)
36+
}
14037

141-
```go
142-
package main
38+
id, err := cache.Fetch("user_id")
39+
if err != nil {
40+
log.Fatal(err)
41+
}
14342

144-
import (
145-
"github.com/faabiosr/cachego"
146-
)
43+
log.Printf("user id: %s \n", id)
14744

148-
var cache cachego.Cache
45+
keys := cache.FetchMulti([]string{"user_id", "user_name"})
14946

150-
func init() {
151-
cache = NewSyncMap()
152-
}
153-
```
47+
for k, v := range keys {
48+
log.Printf("%s: %s\n", k, v)
49+
}
15450

155-
### BoltDB
51+
if cache.Contains("user_name") {
52+
cache.Delete("user_name")
53+
}
15654

157-
```go
158-
package main
55+
if _, err := cache.Fetch("user_name"); err != nil {
56+
log.Printf("%v\n", err)
57+
}
15958

160-
import (
161-
"github.com/faabiosr/cachego"
162-
bolt "github.com/coreos/bbolt"
163-
)
164-
165-
var cache cachego.Cache
166-
167-
func init() {
168-
db, _ := bolt.Open("cache.db", 0600, nil)
169-
cache = NewBolt(db)
59+
if err := cache.Flush(); err != nil {
60+
log.Fatal(err)
61+
}
17062
}
171-
```
172-
173-
### Chain
174-
175-
```go
176-
package main
177-
178-
import (
179-
"github.com/faabiosr/cachego"
180-
)
18163

182-
var cache cachego.Cache
183-
184-
func init() {
185-
memcached := cachego.NewMemcached(
186-
memcached.New("localhost:11211"),
187-
)
188-
189-
redis := cachego.NewRedis(
190-
redis.NewClient(&redis.Options{
191-
Addr: ":6379",
192-
}),
193-
)
194-
195-
file := cachego.NewFile(
196-
"/cache-dir/"
197-
)
198-
199-
cache = cachego.NewChain(
200-
cachego.NewMap(),
201-
memcached,
202-
redis,
203-
file,
204-
)
205-
}
20664
```
20765

208-
### Usage
66+
## Supported drivers
20967

210-
```go
211-
package main
212-
213-
import (
214-
"github.com/faabiosr/cachego"
215-
"github.com/bradfitz/gomemcache/memcache"
216-
)
217-
218-
func main() {
219-
cache.Save("foo", "bar")
220-
cache.Save("john", "doe")
221-
222-
value, err := cache.Fetch("foo")
223-
224-
multiple := cache.FetchMulti([]string{"foo", "john"})
68+
- [Bolt](/bolt)
69+
- [Chain](/chain)
70+
- [File](/file)
71+
- [Memcached](/memcached)
72+
- [Mongo](/mongo)
73+
- [Redis](/redis)
74+
- [Sqlite3](/sqlite3)
75+
- [Sync](/sync)
22576

226-
if cache.Contains("foo") {
227-
cache.Delete("foo")
228-
}
229-
230-
cache.Flush()
231-
}
232-
```
23377

23478
## Documentation
23579

236-
Read the full documentation at [https://godoc.org/github.com/faabiosr/cachego](https://godoc.org/github.com/faabiosr/cachego).
80+
Read the full documentation at [https://pkg.go.dev/github.com/faabiosr/cachego](https://pkg.go.dev/github.com/faabiosr/cachego).
23781

23882
## Development
23983

24084
### Requirements
24185

242-
- Install [docker](https://docs.docker.com/install/) and [docker-compose](https://docs.docker.com/compose/install/)
243-
- Install [go dep](https://github.com/golang/dep)
86+
- Install [docker](https://docs.docker.com/install/)
87+
- Install [docker-compose](https://docs.docker.com/compose/install/)
24488

24589
### Makefile
24690
```sh
24791
// Clean up
24892
$ make clean
24993

25094
//Run tests and generates html coverage file
251-
make cover
95+
$ make cover
25296

25397
// Up the docker containers for testing
254-
make docker
98+
$ make docker
25599

256100
// Format all go files
257-
make fmt
101+
$ make fmt
258102

259103
//Run linters
260-
make lint
104+
$ make lint
261105

262106
// Run tests
263-
make test
107+
$ make test
264108
```
265109

266110
## License

bolt/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Cachego - BoltDB driver
2+
The drivers uses [etcd-io/bbolt](https://github.com/etcd-io/bbolt) to store the cache data.
3+
4+
## Usage
5+
6+
```go
7+
package main
8+
9+
import (
10+
"log"
11+
"time"
12+
13+
bt "go.etcd.io/bbolt"
14+
15+
"github.com/faabiosr/cachego/bolt"
16+
)
17+
18+
func main() {
19+
db, err := bt.Open("cache.db", 0600, nil)
20+
if err != nil {
21+
log.Fatal(err)
22+
}
23+
24+
cache := bolt.New(db)
25+
26+
if err := cache.Save("user_id", "1", 10*time.Second); err != nil {
27+
log.Fatal(err)
28+
}
29+
30+
id, err := cache.Fetch("user_id")
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
35+
log.Printf("user id: %s \n", id)
36+
}
37+
```

chain/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Cachego - Chain driver
2+
The chain driver deals with multiple driver at same time, it could save the key in multiple drivers
3+
and for fetching the driver will call the first one, if fails it will try the next until fail.
4+
5+
## Usage
6+
7+
```go
8+
package main
9+
10+
import (
11+
"log"
12+
"time"
13+
14+
bt "go.etcd.io/bbolt"
15+
16+
"github.com/faabiosr/cachego/bolt"
17+
"github.com/faabiosr/cachego/chain"
18+
"github.com/faabiosr/cachego/sync"
19+
)
20+
21+
func main() {
22+
db, err := bt.Open("cache.db", 0600, nil)
23+
if err != nil {
24+
log.Fatal(err)
25+
}
26+
27+
cache := chain.New(
28+
bolt.New(db),
29+
sync.New(),
30+
)
31+
32+
if err := cache.Save("user_id", "1", 10*time.Second); err != nil {
33+
log.Fatal(err)
34+
}
35+
36+
id, err := cache.Fetch("user_id")
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
41+
log.Printf("user id: %s \n", id)
42+
}
43+
```

0 commit comments

Comments
 (0)