Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
512531e797 | |||
54cb7188b8 | |||
21d40b8e69 | |||
|
b61b8081b7 | ||
c8d21b4ef4 | |||
195c604a31 | |||
b263b69031 | |||
4459bfa1a0 | |||
b90361e48a |
29
.gitea/workflows/job_lint.yml
Normal file
29
.gitea/workflows/job_lint.yml
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
name: lint
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, reopened, synchronize]
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
filter: 'blob:none'
|
||||||
|
- name: setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
go-version: 'stable'
|
||||||
|
- name: setup deps
|
||||||
|
run: go get -v ./...
|
||||||
|
- name: run lint
|
||||||
|
uses: https://github.com/golangci/golangci-lint-action@v6
|
||||||
|
with:
|
||||||
|
version: 'latest'
|
34
.gitea/workflows/job_test.yml
Normal file
34
.gitea/workflows/job_test.yml
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
name: test
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, reopened, synchronize]
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
filter: 'blob:none'
|
||||||
|
- name: setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
go-version: 'stable'
|
||||||
|
- name: setup deps
|
||||||
|
run: go get -v ./...
|
||||||
|
- name: run test
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
run: go test -mod readonly -v ./...
|
53
.gitea/workflows/job_tests.yml
Normal file
53
.gitea/workflows/job_tests.yml
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
name: test
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, reopened, synchronize]
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- v3
|
||||||
|
- v4
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
filter: 'blob:none'
|
||||||
|
- name: checkout tests
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
ref: master
|
||||||
|
filter: 'blob:none'
|
||||||
|
repository: unistack-org/micro-tests
|
||||||
|
path: micro-tests
|
||||||
|
- name: setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
cache-dependency-path: "**/*.sum"
|
||||||
|
go-version: 'stable'
|
||||||
|
- name: setup go work
|
||||||
|
env:
|
||||||
|
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||||
|
run: |
|
||||||
|
go work init
|
||||||
|
go work use .
|
||||||
|
go work use micro-tests
|
||||||
|
- name: setup deps
|
||||||
|
env:
|
||||||
|
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||||
|
run: go get -v ./...
|
||||||
|
- name: run tests
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||||
|
run: |
|
||||||
|
cd micro-tests
|
||||||
|
go test -mod readonly -v ./... || true
|
19
.github/dependabot.yml
vendored
19
.github/dependabot.yml
vendored
@@ -1,19 +0,0 @@
|
|||||||
# To get started with Dependabot version updates, you'll need to specify which
|
|
||||||
# package ecosystems to update and where the package manifests are located.
|
|
||||||
# Please see the documentation for all configuration options:
|
|
||||||
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
|
||||||
|
|
||||||
version: 2
|
|
||||||
updates:
|
|
||||||
|
|
||||||
# Maintain dependencies for GitHub Actions
|
|
||||||
- package-ecosystem: "github-actions"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
||||||
|
|
||||||
# Maintain dependencies for Golang
|
|
||||||
- package-ecosystem: "gomod"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
20
.github/workflows/autoapprove.yml
vendored
20
.github/workflows/autoapprove.yml
vendored
@@ -1,20 +0,0 @@
|
|||||||
name: "autoapprove"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
autoapprove:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: approve
|
|
||||||
uses: hmarr/auto-approve-action@v3
|
|
||||||
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
|
|
||||||
id: approve
|
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
21
.github/workflows/automerge.yml
vendored
21
.github/workflows/automerge.yml
vendored
@@ -1,21 +0,0 @@
|
|||||||
name: "automerge"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
automerge:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
if: github.actor == 'vtolstov'
|
|
||||||
steps:
|
|
||||||
- name: merge
|
|
||||||
id: merge
|
|
||||||
run: gh pr merge --auto --merge "$PR_URL"
|
|
||||||
env:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
47
.github/workflows/build.yml
vendored
47
.github/workflows/build.yml
vendored
@@ -1,47 +0,0 @@
|
|||||||
name: build
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: cache
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: ${{ runner.os }}-go-
|
|
||||||
- name: deps
|
|
||||||
run: go get -v -t -d ./...
|
|
||||||
- name: test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
||||||
lint:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v3.4.0
|
|
||||||
continue-on-error: true
|
|
||||||
with:
|
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
|
||||||
version: v1.30
|
|
||||||
# Optional: working directory, useful for monorepos
|
|
||||||
# working-directory: somedir
|
|
||||||
# Optional: golangci-lint command line arguments.
|
|
||||||
# args: --issues-exit-code=0
|
|
||||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
|
||||||
# only-new-issues: true
|
|
78
.github/workflows/codeql-analysis.yml
vendored
78
.github/workflows/codeql-analysis.yml
vendored
@@ -1,78 +0,0 @@
|
|||||||
# For most projects, this workflow file will not need changing; you simply need
|
|
||||||
# to commit it to your repository.
|
|
||||||
#
|
|
||||||
# You may wish to alter this file to override the set of languages analyzed,
|
|
||||||
# or to provide custom queries or build logic.
|
|
||||||
#
|
|
||||||
# ******** NOTE ********
|
|
||||||
# We have attempted to detect the languages in your repository. Please check
|
|
||||||
# the `language` matrix defined below to confirm you have the correct set of
|
|
||||||
# supported CodeQL languages.
|
|
||||||
#
|
|
||||||
name: "codeql"
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_run:
|
|
||||||
workflows: ["prbuild"]
|
|
||||||
types:
|
|
||||||
- completed
|
|
||||||
push:
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
pull_request:
|
|
||||||
# The branches below must be a subset of the branches above
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
schedule:
|
|
||||||
- cron: '34 1 * * 0'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
analyze:
|
|
||||||
name: analyze
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
actions: read
|
|
||||||
contents: read
|
|
||||||
security-events: write
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
language: [ 'go' ]
|
|
||||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
|
||||||
# Learn more:
|
|
||||||
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
|
||||||
- name: init
|
|
||||||
uses: github/codeql-action/init@v2
|
|
||||||
with:
|
|
||||||
languages: ${{ matrix.language }}
|
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
||||||
# By default, queries listed here will override any specified in a config file.
|
|
||||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
|
||||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
|
||||||
|
|
||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
|
||||||
- name: autobuild
|
|
||||||
uses: github/codeql-action/autobuild@v2
|
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
|
||||||
# 📚 https://git.io/JvXDl
|
|
||||||
|
|
||||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
|
||||||
# and modify them (or add more) to build your code if your project
|
|
||||||
# uses a compiled language
|
|
||||||
|
|
||||||
#- run: |
|
|
||||||
# make bootstrap
|
|
||||||
# make release
|
|
||||||
|
|
||||||
- name: analyze
|
|
||||||
uses: github/codeql-action/analyze@v2
|
|
27
.github/workflows/dependabot-automerge.yml
vendored
27
.github/workflows/dependabot-automerge.yml
vendored
@@ -1,27 +0,0 @@
|
|||||||
name: "dependabot-automerge"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types: [assigned, opened, synchronize, reopened]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
pull-requests: write
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
automerge:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
if: github.actor == 'dependabot[bot]'
|
|
||||||
steps:
|
|
||||||
- name: metadata
|
|
||||||
id: metadata
|
|
||||||
uses: dependabot/fetch-metadata@v1.3.6
|
|
||||||
with:
|
|
||||||
github-token: "${{ secrets.TOKEN }}"
|
|
||||||
- name: merge
|
|
||||||
id: merge
|
|
||||||
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
|
|
||||||
run: gh pr merge --auto --merge "$PR_URL"
|
|
||||||
env:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
47
.github/workflows/pr.yml
vendored
47
.github/workflows/pr.yml
vendored
@@ -1,47 +0,0 @@
|
|||||||
name: prbuild
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: cache
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: ${{ runner.os }}-go-
|
|
||||||
- name: deps
|
|
||||||
run: go get -v -t -d ./...
|
|
||||||
- name: test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
||||||
lint:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v3.4.0
|
|
||||||
continue-on-error: true
|
|
||||||
with:
|
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
|
||||||
version: v1.30
|
|
||||||
# Optional: working directory, useful for monorepos
|
|
||||||
# working-directory: somedir
|
|
||||||
# Optional: golangci-lint command line arguments.
|
|
||||||
# args: --issues-exit-code=0
|
|
||||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
|
||||||
# only-new-issues: true
|
|
5
.golangci.yml
Normal file
5
.golangci.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
run:
|
||||||
|
concurrency: 8
|
||||||
|
deadline: 5m
|
||||||
|
issues-exit-code: 1
|
||||||
|
tests: true
|
4
go.mod
4
go.mod
@@ -1,10 +1,10 @@
|
|||||||
module go.unistack.org/micro-store-redis/v3
|
module go.unistack.org/micro-store-redis/v4
|
||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/redis/go-redis/v9 v9.2.1
|
github.com/redis/go-redis/v9 v9.2.1
|
||||||
go.unistack.org/micro/v3 v3.10.62
|
go.unistack.org/micro/v4 v4.0.7
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
4
go.sum
4
go.sum
@@ -8,5 +8,5 @@ github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaR
|
|||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg=
|
github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg=
|
||||||
github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
||||||
go.unistack.org/micro/v3 v3.10.62 h1:PCwLSt3W53UGosH/5qU3kU0iJxK8jlKOm9p4v/Zti5o=
|
go.unistack.org/micro/v4 v4.0.7 h1:2lwtZlHcSwgkahhFbkI4x1lOS79lw8uLHtcEhlFF+AM=
|
||||||
go.unistack.org/micro/v3 v3.10.62/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
|
go.unistack.org/micro/v4 v4.0.7/go.mod h1:bVEYTlPi0EsdgZZt311bIroDg9ict7ky3C87dSCCAGk=
|
||||||
|
14
options.go
14
options.go
@@ -2,17 +2,11 @@ package redis
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
"go.unistack.org/micro/v3/store"
|
"go.unistack.org/micro/v4/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
type configKey struct{}
|
type universalConfigKey struct{}
|
||||||
|
|
||||||
func Config(c *redis.Options) store.Option {
|
func UniversalConfig(c *redis.UniversalOptions) options.Option {
|
||||||
return store.SetOption(configKey{}, c)
|
return options.ContextOption(universalConfigKey{}, c)
|
||||||
}
|
|
||||||
|
|
||||||
type clusterConfigKey struct{}
|
|
||||||
|
|
||||||
func ClusterConfig(c *redis.ClusterOptions) store.Option {
|
|
||||||
return store.SetOption(clusterConfigKey{}, c)
|
|
||||||
}
|
}
|
||||||
|
527
redis.go
527
redis.go
@@ -7,198 +7,116 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
redis "github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
"go.unistack.org/micro/v3/semconv"
|
"go.unistack.org/micro/v4/options"
|
||||||
"go.unistack.org/micro/v3/store"
|
"go.unistack.org/micro/v4/store"
|
||||||
pool "go.unistack.org/micro/v3/util/xpool"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
DefaultPathSeparator = "/"
|
|
||||||
|
|
||||||
DefaultClusterOptions = &redis.ClusterOptions{
|
|
||||||
Username: "",
|
|
||||||
Password: "", // no password set
|
|
||||||
MaxRetries: 2,
|
|
||||||
MaxRetryBackoff: 256 * time.Millisecond,
|
|
||||||
DialTimeout: 1 * time.Second,
|
|
||||||
ReadTimeout: 1 * time.Second,
|
|
||||||
WriteTimeout: 1 * time.Second,
|
|
||||||
PoolTimeout: 1 * time.Second,
|
|
||||||
MinIdleConns: 10,
|
|
||||||
}
|
|
||||||
|
|
||||||
DefaultOptions = &redis.Options{
|
|
||||||
Username: "",
|
|
||||||
Password: "", // no password set
|
|
||||||
DB: 0, // use default DB
|
|
||||||
MaxRetries: 2,
|
|
||||||
MaxRetryBackoff: 256 * time.Millisecond,
|
|
||||||
DialTimeout: 1 * time.Second,
|
|
||||||
ReadTimeout: 1 * time.Second,
|
|
||||||
WriteTimeout: 1 * time.Second,
|
|
||||||
PoolTimeout: 1 * time.Second,
|
|
||||||
MinIdleConns: 10,
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Store struct {
|
type Store struct {
|
||||||
opts store.Options
|
opts store.Options
|
||||||
cli redisClient
|
cli redis.UniversalClient
|
||||||
pool pool.Pool[strings.Builder]
|
|
||||||
}
|
|
||||||
|
|
||||||
type redisClient interface {
|
|
||||||
Get(ctx context.Context, key string) *redis.StringCmd
|
|
||||||
Del(ctx context.Context, keys ...string) *redis.IntCmd
|
|
||||||
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
|
|
||||||
Keys(ctx context.Context, pattern string) *redis.StringSliceCmd
|
|
||||||
MGet(ctx context.Context, keys ...string) *redis.SliceCmd
|
|
||||||
MSet(ctx context.Context, kv ...interface{}) *redis.StatusCmd
|
|
||||||
Exists(ctx context.Context, keys ...string) *redis.IntCmd
|
|
||||||
Ping(ctx context.Context) *redis.StatusCmd
|
|
||||||
Pipeline() redis.Pipeliner
|
|
||||||
Pipelined(ctx context.Context, fn func(redis.Pipeliner) error) ([]redis.Cmder, error)
|
|
||||||
Close() error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Connect(ctx context.Context) error {
|
func (r *Store) Connect(ctx context.Context) error {
|
||||||
return r.cli.Ping(ctx).Err()
|
return r.cli.Ping(ctx).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Init(opts ...store.Option) error {
|
func (r *Store) Init(opts ...options.Option) error {
|
||||||
for _, o := range opts {
|
return r.configure(opts...)
|
||||||
o(&r.opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
return r.configure()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Redis() *redis.Client {
|
func (r *Store) Redis() redis.UniversalClient {
|
||||||
return r.cli.(*redis.Client)
|
return r.cli
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Disconnect(ctx context.Context) error {
|
func (r *Store) Disconnect(ctx context.Context) error {
|
||||||
return r.cli.Close()
|
return r.cli.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
func (r *Store) Exists(ctx context.Context, key string, opts ...options.Option) error {
|
||||||
options := store.NewExistsOptions(opts...)
|
if r.opts.Timeout > 0 {
|
||||||
|
|
||||||
timeout := r.opts.Timeout
|
|
||||||
if options.Timeout > 0 {
|
|
||||||
timeout = options.Timeout
|
|
||||||
}
|
|
||||||
|
|
||||||
if timeout > 0 {
|
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
|
options := store.NewExistsOptions(opts...)
|
||||||
rkey := r.getKey(r.opts.Namespace, options.Namespace, key)
|
if len(options.Namespace) == 0 {
|
||||||
ctx, sp := r.opts.Tracer.Start(ctx, "cache read "+rkey)
|
options.Namespace = r.opts.Namespace
|
||||||
defer sp.Finish()
|
}
|
||||||
|
if options.Namespace != "" {
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Inc()
|
key = fmt.Sprintf("%s%s", r.opts.Namespace, key)
|
||||||
ts := time.Now()
|
}
|
||||||
val, err := r.cli.Exists(ctx, rkey).Result()
|
val, err := r.cli.Exists(ctx, key).Result()
|
||||||
te := time.Since(ts)
|
if err != nil {
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Dec()
|
|
||||||
r.opts.Meter.Summary(semconv.CacheRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
r.opts.Meter.Histogram(semconv.CacheRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
if err == redis.Nil || (err == nil && val == 0) {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "miss").Inc()
|
|
||||||
return store.ErrNotFound
|
|
||||||
} else if err == nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "hit").Inc()
|
|
||||||
} else if err != nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "failure").Inc()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if val == 0 {
|
||||||
|
return store.ErrNotFound
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
|
func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...options.Option) error {
|
||||||
options := store.NewReadOptions(opts...)
|
if r.opts.Timeout > 0 {
|
||||||
|
|
||||||
timeout := r.opts.Timeout
|
|
||||||
if options.Timeout > 0 {
|
|
||||||
timeout = options.Timeout
|
|
||||||
}
|
|
||||||
|
|
||||||
if timeout > 0 {
|
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
|
options := store.NewReadOptions(opts...)
|
||||||
|
if len(options.Namespace) == 0 {
|
||||||
|
options.Namespace = r.opts.Namespace
|
||||||
|
}
|
||||||
|
if options.Namespace != "" {
|
||||||
|
key = fmt.Sprintf("%s%s", options.Namespace, key)
|
||||||
|
}
|
||||||
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Inc()
|
buf, err := r.cli.Get(ctx, key).Bytes()
|
||||||
ts := time.Now()
|
if err != nil && err == redis.Nil {
|
||||||
buf, err := r.cli.Get(ctx, r.getKey(r.opts.Namespace, options.Namespace, key)).Bytes()
|
|
||||||
te := time.Since(ts)
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Dec()
|
|
||||||
r.opts.Meter.Summary(semconv.CacheRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
r.opts.Meter.Histogram(semconv.CacheRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
if err == redis.Nil || (err == nil && buf == nil) {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "miss").Inc()
|
|
||||||
return store.ErrNotFound
|
return store.ErrNotFound
|
||||||
} else if err == nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "hit").Inc()
|
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "failure").Inc()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if buf == nil {
|
||||||
switch b := val.(type) {
|
return store.ErrNotFound
|
||||||
case *[]byte:
|
|
||||||
*b = buf
|
|
||||||
case *string:
|
|
||||||
*b = string(buf)
|
|
||||||
default:
|
|
||||||
err = r.opts.Codec.Unmarshal(buf, val)
|
|
||||||
}
|
}
|
||||||
|
return r.opts.Codec.Unmarshal(buf, val)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts ...store.ReadOption) error {
|
func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts ...options.Option) error {
|
||||||
options := store.NewReadOptions(opts...)
|
if len(keys) == 1 {
|
||||||
|
vt := reflect.ValueOf(vals)
|
||||||
timeout := r.opts.Timeout
|
if vt.Kind() == reflect.Ptr {
|
||||||
if options.Timeout > 0 {
|
vt = reflect.Indirect(vt)
|
||||||
timeout = options.Timeout
|
return r.Read(ctx, keys[0], vt.Index(0).Interface(), opts...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if r.opts.Timeout > 0 {
|
||||||
if timeout > 0 {
|
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
|
options := store.NewReadOptions(opts...)
|
||||||
if r.opts.Namespace != "" || options.Namespace != "" {
|
rkeys := make([]string, 0, len(keys))
|
||||||
for idx, key := range keys {
|
if len(options.Namespace) == 0 {
|
||||||
keys[idx] = r.getKey(r.opts.Namespace, options.Namespace, key)
|
options.Namespace = r.opts.Namespace
|
||||||
|
}
|
||||||
|
for _, key := range keys {
|
||||||
|
if options.Namespace != "" {
|
||||||
|
rkeys = append(rkeys, fmt.Sprintf("%s%s", options.Namespace, key))
|
||||||
|
} else {
|
||||||
|
rkeys = append(rkeys, key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Inc()
|
rvals, err := r.cli.MGet(ctx, rkeys...).Result()
|
||||||
ts := time.Now()
|
if err != nil && err == redis.Nil {
|
||||||
rvals, err := r.cli.MGet(ctx, keys...).Result()
|
|
||||||
te := time.Since(ts)
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Dec()
|
|
||||||
r.opts.Meter.Summary(semconv.CacheRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
r.opts.Meter.Histogram(semconv.CacheRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
if err == redis.Nil || (len(rvals) == 0) {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "miss").Inc()
|
|
||||||
return store.ErrNotFound
|
return store.ErrNotFound
|
||||||
} else if err == nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "hit").Inc()
|
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "failure").Inc()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if len(rvals) == 0 {
|
||||||
|
return store.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
vv := reflect.ValueOf(vals)
|
vv := reflect.ValueOf(vals)
|
||||||
vt := reflect.TypeOf(vals)
|
vt := reflect.TypeOf(vals)
|
||||||
@@ -228,14 +146,9 @@ func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts
|
|||||||
// special case for raw data
|
// special case for raw data
|
||||||
if vt.Kind() == reflect.Slice && vt.Elem().Kind() == reflect.Uint8 {
|
if vt.Kind() == reflect.Slice && vt.Elem().Kind() == reflect.Uint8 {
|
||||||
itm.Set(reflect.MakeSlice(itm.Type(), len(buf), len(buf)))
|
itm.Set(reflect.MakeSlice(itm.Type(), len(buf), len(buf)))
|
||||||
itm.SetBytes(buf)
|
} else {
|
||||||
continue
|
itm.Set(reflect.New(vt.Elem()))
|
||||||
} else if vt.Kind() == reflect.String {
|
|
||||||
itm.SetString(string(buf))
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
itm.Set(reflect.New(vt.Elem()))
|
|
||||||
if err = r.opts.Codec.Unmarshal(buf, itm.Interface()); err != nil {
|
if err = r.opts.Codec.Unmarshal(buf, itm.Interface()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -244,98 +157,66 @@ func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.DeleteOption) error {
|
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...options.Option) error {
|
||||||
options := store.NewDeleteOptions(opts...)
|
if len(keys) == 1 {
|
||||||
|
return r.Delete(ctx, keys[0], opts...)
|
||||||
timeout := r.opts.Timeout
|
|
||||||
if options.Timeout > 0 {
|
|
||||||
timeout = options.Timeout
|
|
||||||
}
|
}
|
||||||
|
if r.opts.Timeout > 0 {
|
||||||
if timeout > 0 {
|
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
|
options := store.NewDeleteOptions(opts...)
|
||||||
if r.opts.Namespace != "" || options.Namespace != "" {
|
if len(options.Namespace) == 0 {
|
||||||
for idx, key := range keys {
|
options.Namespace = r.opts.Namespace
|
||||||
keys[idx] = r.getKey(r.opts.Namespace, options.Namespace, key)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if options.Namespace == "" {
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Inc()
|
return r.cli.Del(ctx, keys...).Err()
|
||||||
ts := time.Now()
|
|
||||||
err := r.cli.Del(ctx, keys...).Err()
|
|
||||||
te := time.Since(ts)
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Dec()
|
|
||||||
r.opts.Meter.Summary(semconv.CacheRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
r.opts.Meter.Histogram(semconv.CacheRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
if err == redis.Nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "miss").Inc()
|
|
||||||
return store.ErrNotFound
|
|
||||||
} else if err == nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "hit").Inc()
|
|
||||||
} else if err != nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "failure").Inc()
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
for idx := range keys {
|
||||||
return nil
|
keys[idx] = options.Namespace + keys[idx]
|
||||||
|
}
|
||||||
|
return r.cli.Del(ctx, keys...).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
|
func (r *Store) Delete(ctx context.Context, key string, opts ...options.Option) error {
|
||||||
options := store.NewDeleteOptions(opts...)
|
if r.opts.Timeout > 0 {
|
||||||
|
|
||||||
timeout := r.opts.Timeout
|
|
||||||
if options.Timeout > 0 {
|
|
||||||
timeout = options.Timeout
|
|
||||||
}
|
|
||||||
|
|
||||||
if timeout > 0 {
|
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
|
options := store.NewDeleteOptions(opts...)
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Inc()
|
if len(options.Namespace) == 0 {
|
||||||
ts := time.Now()
|
options.Namespace = r.opts.Namespace
|
||||||
err := r.cli.Del(ctx, r.getKey(r.opts.Namespace, options.Namespace, key)).Err()
|
|
||||||
te := time.Since(ts)
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Dec()
|
|
||||||
r.opts.Meter.Summary(semconv.CacheRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
r.opts.Meter.Histogram(semconv.CacheRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
if err == redis.Nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "miss").Inc()
|
|
||||||
return store.ErrNotFound
|
|
||||||
} else if err == nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "hit").Inc()
|
|
||||||
} else if err != nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "failure").Inc()
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
if options.Namespace == "" {
|
||||||
return nil
|
return r.cli.Del(ctx, key).Err()
|
||||||
|
}
|
||||||
|
return r.cli.Del(ctx, fmt.Sprintf("%s%s", options.Namespace, key)).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...store.WriteOption) error {
|
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...options.Option) error {
|
||||||
|
if len(keys) == 1 {
|
||||||
|
return r.Write(ctx, keys[0], vals[0], opts...)
|
||||||
|
}
|
||||||
|
if r.opts.Timeout > 0 {
|
||||||
|
var cancel context.CancelFunc
|
||||||
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||||
|
defer cancel()
|
||||||
|
}
|
||||||
options := store.NewWriteOptions(opts...)
|
options := store.NewWriteOptions(opts...)
|
||||||
|
|
||||||
timeout := r.opts.Timeout
|
|
||||||
if options.Timeout > 0 {
|
|
||||||
timeout = options.Timeout
|
|
||||||
}
|
|
||||||
|
|
||||||
if timeout > 0 {
|
|
||||||
var cancel context.CancelFunc
|
|
||||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
|
||||||
defer cancel()
|
|
||||||
}
|
|
||||||
|
|
||||||
kvs := make([]string, 0, len(keys)*2)
|
kvs := make([]string, 0, len(keys)*2)
|
||||||
|
|
||||||
|
if len(options.Namespace) == 0 {
|
||||||
|
options.Namespace = r.opts.Namespace
|
||||||
|
}
|
||||||
|
|
||||||
for idx, key := range keys {
|
for idx, key := range keys {
|
||||||
kvs = append(kvs, r.getKey(r.opts.Namespace, options.Namespace, key))
|
if options.Namespace != "" {
|
||||||
|
kvs = append(kvs, fmt.Sprintf("%s%s", options.Namespace, key))
|
||||||
|
} else {
|
||||||
|
kvs = append(kvs, key)
|
||||||
|
}
|
||||||
|
|
||||||
switch vt := vals[idx].(type) {
|
switch vt := vals[idx].(type) {
|
||||||
case string:
|
case string:
|
||||||
@@ -351,39 +232,21 @@ func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, o
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Inc()
|
|
||||||
ts := time.Now()
|
|
||||||
|
|
||||||
cmds, err := r.cli.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
cmds, err := r.cli.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
||||||
|
var err error
|
||||||
for idx := 0; idx < len(kvs); idx += 2 {
|
for idx := 0; idx < len(kvs); idx += 2 {
|
||||||
if _, err := pipe.Set(ctx, kvs[idx], kvs[idx+1], options.TTL).Result(); err != nil {
|
if _, err = pipe.Set(ctx, kvs[idx], kvs[idx+1], options.TTL).Result(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
te := time.Since(ts)
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Dec()
|
|
||||||
r.opts.Meter.Summary(semconv.CacheRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
r.opts.Meter.Histogram(semconv.CacheRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
if err == redis.Nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "miss").Inc()
|
|
||||||
return store.ErrNotFound
|
|
||||||
} else if err == nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "hit").Inc()
|
|
||||||
} else if err != nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "failure").Inc()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range cmds {
|
||||||
if err = cmd.Err(); err != nil {
|
if err = cmd.Err(); err != nil {
|
||||||
if err == redis.Nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "miss").Inc()
|
|
||||||
return store.ErrNotFound
|
|
||||||
}
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "failure").Inc()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -391,18 +254,16 @@ func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, o
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...options.Option) error {
|
||||||
options := store.NewWriteOptions(opts...)
|
if r.opts.Timeout > 0 {
|
||||||
|
var cancel context.CancelFunc
|
||||||
timeout := r.opts.Timeout
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||||
if options.Timeout > 0 {
|
defer cancel()
|
||||||
timeout = options.Timeout
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if timeout > 0 {
|
options := store.NewWriteOptions(opts...)
|
||||||
var cancel context.CancelFunc
|
if len(options.Namespace) == 0 {
|
||||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
options.Namespace = r.opts.Namespace
|
||||||
defer cancel()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf []byte
|
var buf []byte
|
||||||
@@ -419,79 +280,42 @@ func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Inc()
|
if options.Namespace != "" {
|
||||||
ts := time.Now()
|
key = fmt.Sprintf("%s%s", options.Namespace, key)
|
||||||
err := r.cli.Set(ctx, r.getKey(r.opts.Namespace, options.Namespace, key), buf, options.TTL).Err()
|
|
||||||
te := time.Since(ts)
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Dec()
|
|
||||||
r.opts.Meter.Summary(semconv.CacheRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
r.opts.Meter.Histogram(semconv.CacheRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
if err == redis.Nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "miss").Inc()
|
|
||||||
return store.ErrNotFound
|
|
||||||
} else if err == nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "hit").Inc()
|
|
||||||
} else if err != nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "failure").Inc()
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return r.cli.Set(ctx, key, buf, options.TTL).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
|
func (r *Store) List(ctx context.Context, opts ...options.Option) ([]string, error) {
|
||||||
options := store.NewListOptions(opts...)
|
options := store.NewListOptions(opts...)
|
||||||
if len(options.Namespace) == 0 {
|
if len(options.Namespace) == 0 {
|
||||||
options.Namespace = r.opts.Namespace
|
options.Namespace = r.opts.Namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
rkey := r.getKey(options.Namespace, "", options.Prefix+"*")
|
rkey := fmt.Sprintf("%s%s*", options.Namespace, options.Prefix)
|
||||||
if options.Suffix != "" {
|
if options.Suffix != "" {
|
||||||
rkey += options.Suffix
|
rkey += options.Suffix
|
||||||
}
|
}
|
||||||
|
if r.opts.Timeout > 0 {
|
||||||
timeout := r.opts.Timeout
|
|
||||||
if options.Timeout > 0 {
|
|
||||||
timeout = options.Timeout
|
|
||||||
}
|
|
||||||
|
|
||||||
if timeout > 0 {
|
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add support for prefix/suffix/limit
|
// TODO: add support for prefix/suffix/limit
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Inc()
|
|
||||||
ts := time.Now()
|
|
||||||
keys, err := r.cli.Keys(ctx, rkey).Result()
|
keys, err := r.cli.Keys(ctx, rkey).Result()
|
||||||
te := time.Since(ts)
|
if err != nil {
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestInflight, "name", options.Name).Dec()
|
|
||||||
r.opts.Meter.Summary(semconv.CacheRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
r.opts.Meter.Histogram(semconv.CacheRequestDurationSeconds, "name", options.Name).Update(te.Seconds())
|
|
||||||
if err == redis.Nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "miss").Inc()
|
|
||||||
return nil, store.ErrNotFound
|
|
||||||
} else if err == nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "hit").Inc()
|
|
||||||
} else if err != nil {
|
|
||||||
r.opts.Meter.Counter(semconv.CacheRequestTotal, "name", options.Name, "status", "failure").Inc()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if options.Namespace == "" {
|
||||||
prefix := r.opts.Namespace
|
|
||||||
if options.Namespace != "" {
|
|
||||||
prefix = options.Namespace
|
|
||||||
}
|
|
||||||
if prefix == "" {
|
|
||||||
return keys, nil
|
return keys, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for idx, key := range keys {
|
nkeys := make([]string, 0, len(keys))
|
||||||
keys[idx] = strings.TrimPrefix(key, prefix)
|
for _, key := range keys {
|
||||||
|
nkeys = append(nkeys, strings.TrimPrefix(key, options.Namespace))
|
||||||
}
|
}
|
||||||
|
return nkeys, nil
|
||||||
return keys, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) Options() store.Options {
|
func (r *Store) Options() store.Options {
|
||||||
@@ -506,83 +330,60 @@ func (r *Store) String() string {
|
|||||||
return "redis"
|
return "redis"
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStore(opts ...store.Option) *Store {
|
func NewStore(opts ...options.Option) *Store {
|
||||||
return &Store{opts: store.NewOptions(opts...)}
|
return &Store{opts: store.NewOptions(opts...)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) configure() error {
|
func (r *Store) configure(opts ...options.Option) error {
|
||||||
var redisOptions *redis.Options
|
if r.cli != nil && len(opts) == 0 {
|
||||||
var redisClusterOptions *redis.ClusterOptions
|
return nil
|
||||||
var err error
|
|
||||||
|
|
||||||
nodes := r.opts.Addrs
|
|
||||||
|
|
||||||
if len(nodes) == 0 {
|
|
||||||
nodes = []string{"redis://127.0.0.1:6379"}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.cli != nil && r.opts.Context == nil {
|
var redisUniversalOptions *redis.UniversalOptions
|
||||||
return nil
|
var err error
|
||||||
|
|
||||||
|
for _, o := range opts {
|
||||||
|
if err = o(r.opts); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.opts.Context != nil {
|
if r.opts.Context != nil {
|
||||||
if c, ok := r.opts.Context.Value(configKey{}).(*redis.Options); ok {
|
if c, ok := r.opts.Context.Value(universalConfigKey{}).(*redis.UniversalOptions); ok {
|
||||||
redisOptions = c
|
redisUniversalOptions = c
|
||||||
if r.opts.TLSConfig != nil {
|
if r.opts.TLSConfig != nil {
|
||||||
redisOptions.TLSConfig = r.opts.TLSConfig
|
redisUniversalOptions.TLSConfig = r.opts.TLSConfig
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if c, ok := r.opts.Context.Value(clusterConfigKey{}).(*redis.ClusterOptions); ok {
|
|
||||||
redisClusterOptions = c
|
|
||||||
if r.opts.TLSConfig != nil {
|
|
||||||
redisClusterOptions.TLSConfig = r.opts.TLSConfig
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if redisOptions != nil && redisClusterOptions != nil {
|
if redisUniversalOptions == nil && r.cli != nil {
|
||||||
return fmt.Errorf("must specify only one option Config or ClusterConfig")
|
|
||||||
}
|
|
||||||
|
|
||||||
if redisOptions == nil && redisClusterOptions == nil && r.cli != nil {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if redisOptions == nil && redisClusterOptions == nil && len(nodes) == 1 {
|
if redisUniversalOptions == nil {
|
||||||
redisOptions, err = redis.ParseURL(nodes[0])
|
redisUniversalOptions = &redis.UniversalOptions{
|
||||||
if err != nil {
|
Username: "",
|
||||||
redisOptions = DefaultOptions
|
Password: "", // no password set
|
||||||
redisOptions.Addr = r.opts.Addrs[0]
|
DB: 0, // use default DB
|
||||||
redisOptions.TLSConfig = r.opts.TLSConfig
|
MaxRetries: 2,
|
||||||
|
MaxRetryBackoff: 256 * time.Millisecond,
|
||||||
|
DialTimeout: 1 * time.Second,
|
||||||
|
ReadTimeout: 1 * time.Second,
|
||||||
|
WriteTimeout: 1 * time.Second,
|
||||||
|
PoolTimeout: 1 * time.Second,
|
||||||
|
MinIdleConns: 10,
|
||||||
|
TLSConfig: r.opts.TLSConfig,
|
||||||
}
|
}
|
||||||
} else if redisOptions == nil && redisClusterOptions == nil && len(nodes) > 1 {
|
|
||||||
redisClusterOptions = DefaultClusterOptions
|
|
||||||
redisClusterOptions.Addrs = r.opts.Addrs
|
|
||||||
redisClusterOptions.TLSConfig = r.opts.TLSConfig
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if redisOptions != nil {
|
if len(r.opts.Address) > 0 {
|
||||||
r.cli = redis.NewClient(redisOptions)
|
redisUniversalOptions.Addrs = r.opts.Address
|
||||||
} else if redisClusterOptions != nil {
|
} else if len(redisUniversalOptions.Addrs) == 0 {
|
||||||
r.cli = redis.NewClusterClient(redisClusterOptions)
|
redisUniversalOptions.Addrs = []string{"redis://127.0.0.1:6379"}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r.cli = redis.NewUniversalClient(redisUniversalOptions)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Store) getKey(mainNamespace string, opNamespace string, key string) string {
|
|
||||||
b := r.pool.Get()
|
|
||||||
defer r.pool.Put(b)
|
|
||||||
b.Reset()
|
|
||||||
|
|
||||||
if opNamespace == "" {
|
|
||||||
opNamespace = mainNamespace
|
|
||||||
}
|
|
||||||
if opNamespace != "" {
|
|
||||||
b.WriteString(opNamespace)
|
|
||||||
b.WriteString(DefaultPathSeparator)
|
|
||||||
}
|
|
||||||
b.WriteString(key)
|
|
||||||
return b.String()
|
|
||||||
}
|
|
||||||
|
@@ -8,7 +8,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
"go.unistack.org/micro/v3/store"
|
"go.unistack.org/micro/v4/options"
|
||||||
|
"go.unistack.org/micro/v4/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_rkv_configure(t *testing.T) {
|
func Test_rkv_configure(t *testing.T) {
|
||||||
@@ -37,7 +38,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "legacy Url", fields: fields{options: store.Options{Addrs: []string{"127.0.0.1:6379"}}, Client: nil},
|
name: "legacy Url", fields: fields{options: store.Options{Address: []string{"127.0.0.1:6379"}}, Client: nil},
|
||||||
wantErr: false, want: wantValues{
|
wantErr: false, want: wantValues{
|
||||||
username: "",
|
username: "",
|
||||||
password: "",
|
password: "",
|
||||||
@@ -45,7 +46,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "New Url", fields: fields{options: store.Options{Addrs: []string{"redis://127.0.0.1:6379"}}, Client: nil},
|
name: "New Url", fields: fields{options: store.Options{Address: []string{"redis://127.0.0.1:6379"}}, Client: nil},
|
||||||
wantErr: false, want: wantValues{
|
wantErr: false, want: wantValues{
|
||||||
username: "",
|
username: "",
|
||||||
password: "",
|
password: "",
|
||||||
@@ -53,7 +54,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Url with Pwd", fields: fields{options: store.Options{Addrs: []string{"redis://:password@redis:6379"}}, Client: nil},
|
name: "Url with Pwd", fields: fields{options: store.Options{Address: []string{"redis://:password@redis:6379"}}, Client: nil},
|
||||||
wantErr: false, want: wantValues{
|
wantErr: false, want: wantValues{
|
||||||
username: "",
|
username: "",
|
||||||
password: "password",
|
password: "password",
|
||||||
@@ -61,7 +62,7 @@ func Test_rkv_configure(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Url with username and Pwd", fields: fields{options: store.Options{Addrs: []string{"redis://username:password@redis:6379"}}, Client: nil},
|
name: "Url with username and Pwd", fields: fields{options: store.Options{Address: []string{"redis://username:password@redis:6379"}}, Client: nil},
|
||||||
wantErr: false, want: wantValues{
|
wantErr: false, want: wantValues{
|
||||||
username: "username",
|
username: "username",
|
||||||
password: "password",
|
password: "password",
|
||||||
@@ -90,7 +91,7 @@ func Test_Store(t *testing.T) {
|
|||||||
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
||||||
t.Skip()
|
t.Skip()
|
||||||
}
|
}
|
||||||
r := NewStore(store.Addrs(os.Getenv("STORE_NODES")))
|
r := NewStore(options.Address(os.Getenv("STORE_NODES")))
|
||||||
|
|
||||||
if err := r.Init(); err != nil {
|
if err := r.Init(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -130,7 +131,7 @@ func Test_MRead(t *testing.T) {
|
|||||||
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
||||||
t.Skip()
|
t.Skip()
|
||||||
}
|
}
|
||||||
r := NewStore(store.Addrs(os.Getenv("STORE_NODES")))
|
r := NewStore(options.Address(os.Getenv("STORE_NODES")))
|
||||||
|
|
||||||
if err = r.Init(); err != nil {
|
if err = r.Init(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
Reference in New Issue
Block a user