summaryrefslogtreecommitdiff
path: root/tests/plugins/redis/plugin1.go
blob: 68da1394e9f1359b03b852d4f9d27bd42c0342aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package redis

import (
	"context"
	"time"

	"github.com/go-redis/redis/v8"
	"github.com/spiral/errors"
	redisPlugin "github.com/spiral/roadrunner/v2/plugins/redis"
)

type Plugin1 struct {
	redisClient redis.UniversalClient
}

func (p *Plugin1) Init(redis redisPlugin.Redis) error {
	var err error
	p.redisClient, err = redis.RedisClient("redis")

	return err
}

func (p *Plugin1) Serve() chan error {
	const op = errors.Op("plugin1 serve")
	errCh := make(chan error, 1)
	p.redisClient.Set(context.Background(), "foo", "bar", time.Minute)

	stringCmd := p.redisClient.Get(context.Background(), "foo")
	data, err := stringCmd.Result()
	if err != nil {
		errCh <- errors.E(op, err)
		return errCh
	}

	if data != "bar" {
		errCh <- errors.E(op, errors.Str("no such key"))
		return errCh
	}

	return errCh
}

func (p *Plugin1) Stop() error {
	return nil
}