-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add redis backend and provide examples (#5)
- Loading branch information
Showing
10 changed files
with
234 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name: Remove old artifacts | ||
|
||
on: | ||
schedule: | ||
- cron: '0 1 * * *' | ||
|
||
jobs: | ||
remove-old-artifacts: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 10 | ||
|
||
steps: | ||
- name: Remove old artifacts | ||
uses: c-hive/gha-remove-artifacts@v1 | ||
with: | ||
age: '1 month' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,113 @@ | ||
package backends | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"io/ioutil" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/go-redis/redis" | ||
) | ||
|
||
var ( | ||
client *redis.Client | ||
) | ||
|
||
// RedisBackend saves the content into redis | ||
type RedisBackend struct { | ||
Ctx context.Context | ||
Key string | ||
content bytes.Buffer | ||
expiration time.Time | ||
} | ||
|
||
func ParRedisConfig(connSetting string) (*redis.Options, error) { | ||
var err error | ||
addr, password, db := "localhost:6379", "", 0 | ||
|
||
args := strings.Split(connSetting, " ") | ||
length := len(args) | ||
// the format of args: addr db password | ||
|
||
addr = args[0] | ||
|
||
if length > 1 { | ||
db, err = strconv.Atoi(args[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if length > 2 { | ||
password = args[2] | ||
} | ||
|
||
return &redis.Options{ | ||
Addr: addr, | ||
DB: db, | ||
Password: password, | ||
}, nil | ||
} | ||
|
||
// InitRedisClient init the client for the redis | ||
func InitRedisClient(addr, password string, db int) error { | ||
l.Lock() | ||
defer l.Unlock() | ||
|
||
client = redis.NewClient(&redis.Options{ | ||
Addr: addr, | ||
Password: password, | ||
DB: db, | ||
}) | ||
|
||
if _, err := client.Ping().Result(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// NewRedisBackend new a redis backend for cache's storage | ||
func NewRedisBackend(ctx context.Context, key string, expiration time.Time) (Backend, error) { | ||
return &RedisBackend{ | ||
Ctx: ctx, | ||
Key: key, | ||
expiration: expiration, | ||
}, nil | ||
} | ||
|
||
// Write writes the response content in a temp buffer | ||
func (r *RedisBackend) Write(p []byte) (n int, err error) { | ||
return r.content.Write(p) | ||
} | ||
|
||
// Flush do nothing here | ||
func (r *RedisBackend) Flush() error { | ||
return nil | ||
} | ||
|
||
// Close writeh the temp buffer's content to the groupcache | ||
func (r *RedisBackend) Close() error { | ||
_, err := client.Set(r.Key, r.content.Bytes(), r.expiration.Sub(time.Now())).Result() | ||
return err | ||
} | ||
|
||
// Clean performs the purge storage | ||
func (r *RedisBackend) Clean() error { | ||
_, err := client.Del(r.Key).Result() | ||
return err | ||
} | ||
|
||
// GetReader return a reader for the write public response | ||
func (r *RedisBackend) GetReader() (io.ReadCloser, error) { | ||
content, err := client.Get(r.Key).Result() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rc := ioutil.NopCloser(strings.NewReader(content)) | ||
return rc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
order http_cache before reverse_proxy | ||
} | ||
|
||
:9991 { | ||
reverse_proxy { | ||
to localhost:9995 | ||
header_up Host {http.reverse_proxy.upstream.host} | ||
} | ||
|
||
http_cache { | ||
cache_type redis | ||
redis_connection_setting localhost:6379 | ||
match_path / | ||
} | ||
} | ||
|
||
|
||
:9995 { | ||
header Cache-control "public" | ||
root * /tmp/caddy-benchmark | ||
file_server | ||
|
||
log { | ||
level info | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters