Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed support for redis v9 #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions redisstorage.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package redisstorage

import (
"context"
"fmt"
"log"
"net/url"
"sync"
"time"

"github.com/go-redis/redis"
"github.com/go-redis/redis/v9"
)

var (
ctx = context.Background()
)

// Storage implements the redis storage backend for Colly
Expand Down Expand Up @@ -40,7 +45,7 @@ func (s *Storage) Init() error {
DB: s.DB,
})
}
_, err := s.Client.Ping().Result()
_, err := s.Client.Ping(ctx).Result()
if err != nil {
return fmt.Errorf("Redis connection error: %s", err.Error())
}
Expand All @@ -51,29 +56,29 @@ func (s *Storage) Init() error {
func (s *Storage) Clear() error {
s.mu.Lock()
defer s.mu.Unlock()
r := s.Client.Keys(s.getCookieID("*"))
r := s.Client.Keys(ctx, s.getCookieID("*"))
keys, err := r.Result()
if err != nil {
return err
}
r2 := s.Client.Keys(s.Prefix + ":request:*")
r2 := s.Client.Keys(ctx, s.Prefix+":request:*")
keys2, err := r2.Result()
if err != nil {
return err
}
keys = append(keys, keys2...)
keys = append(keys, s.getQueueID())
return s.Client.Del(keys...).Err()
return s.Client.Del(ctx, keys...).Err()
}

// Visited implements colly/storage.Visited()
func (s *Storage) Visited(requestID uint64) error {
return s.Client.Set(s.getIDStr(requestID), "1", s.Expires).Err()
return s.Client.Set(ctx, s.getIDStr(requestID), "1", s.Expires).Err()
}

// IsVisited implements colly/storage.IsVisited()
func (s *Storage) IsVisited(requestID uint64) (bool, error) {
_, err := s.Client.Get(s.getIDStr(requestID)).Result()
_, err := s.Client.Get(ctx, s.getIDStr(requestID)).Result()
if err == redis.Nil {
return false, nil
} else if err != nil {
Expand All @@ -93,7 +98,7 @@ func (s *Storage) SetCookies(u *url.URL, cookies string) {
s.mu.Lock()
defer s.mu.Unlock()
// return s.Client.Set(s.getCookieID(u.Host), stringify(cnew), 0).Err()
err := s.Client.Set(s.getCookieID(u.Host), cookies, 0).Err()
err := s.Client.Set(ctx, s.getCookieID(u.Host), cookies, 0).Err()
if err != nil {
// return nil
log.Printf("SetCookies() .Set error %s", err)
Expand All @@ -106,7 +111,7 @@ func (s *Storage) Cookies(u *url.URL) string {
// TODO(js) Cookie methods currently have no way to return an error.

s.mu.RLock()
cookiesStr, err := s.Client.Get(s.getCookieID(u.Host)).Result()
cookiesStr, err := s.Client.Get(ctx, s.getCookieID(u.Host)).Result()
s.mu.RUnlock()
if err == redis.Nil {
cookiesStr = ""
Expand All @@ -120,12 +125,12 @@ func (s *Storage) Cookies(u *url.URL) string {

// AddRequest implements queue.Storage.AddRequest() function
func (s *Storage) AddRequest(r []byte) error {
return s.Client.RPush(s.getQueueID(), r).Err()
return s.Client.RPush(ctx, s.getQueueID(), r).Err()
}

// GetRequest implements queue.Storage.GetRequest() function
func (s *Storage) GetRequest() ([]byte, error) {
r, err := s.Client.LPop(s.getQueueID()).Bytes()
r, err := s.Client.LPop(ctx, s.getQueueID()).Bytes()
if err != nil {
return nil, err
}
Expand All @@ -134,7 +139,7 @@ func (s *Storage) GetRequest() ([]byte, error) {

// QueueSize implements queue.Storage.QueueSize() function
func (s *Storage) QueueSize() (int, error) {
i, err := s.Client.LLen(s.getQueueID()).Result()
i, err := s.Client.LLen(ctx, s.getQueueID()).Result()
return int(i), err
}

Expand Down