redis

package
v0.24.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package redis contains gotd storage implementations using Redis.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessHasher

type AccessHasher struct {
	// contains filtered or unexported fields
}

AccessHasher stores channel access hashes in Redis

func NewAccessHasher

func NewAccessHasher(client *redis.Client, prefix string) *AccessHasher

NewAccessHasher creates a new Redis-backed access hasher. ttl can be 0 for permanent storage.

func (*AccessHasher) GetChannelAccessHash

func (h *AccessHasher) GetChannelAccessHash(ctx context.Context, userID, channelID int64) (int64, bool, error)

func (*AccessHasher) Key

func (h *AccessHasher) Key(userID int64) string

func (*AccessHasher) SetChannelAccessHash

func (h *AccessHasher) SetChannelAccessHash(ctx context.Context, userID, channelID, accessHash int64) error

type Credentials

type Credentials struct {
	kv.Credentials
}

Credentials stores user credentials to Redis.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/go-faster/errors"
	redisclient "github.com/redis/go-redis/v9"

	tgauth "github.com/gotd/td/telegram/auth"

	"github.com/gotd/td/telegram"

	"github.com/gotd/contrib/auth"
	"github.com/gotd/contrib/auth/terminal"
	"github.com/gotd/contrib/redis"
)

func redisAuth(ctx context.Context) error {
	redisClient := redisclient.NewClient(&redisclient.Options{})
	cred := redis.NewCredentials(redisClient).
		WithPhoneKey("phone").
		WithPasswordKey("password")

	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return errors.Errorf("create client: %w", err)
	}

	return client.Run(ctx, func(ctx context.Context) error {
		return client.Auth().IfNecessary(
			ctx,
			tgauth.NewFlow(auth.Build(cred, terminal.OS()), tgauth.SendCodeOptions{}),
		)
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := redisAuth(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(1)
	}
}

func NewCredentials

func NewCredentials(client *redis.Client) Credentials

NewCredentials creates new Credentials.

type PeerStorage

type PeerStorage struct {
	// contains filtered or unexported fields
}

PeerStorage is a peer storage based on redis.

func NewPeerStorage

func NewPeerStorage(client *redis.Client) *PeerStorage

NewPeerStorage creates new peer storage using redis.

func (PeerStorage) Add

func (s PeerStorage) Add(ctx context.Context, value storage.Peer) error

Add adds given peer to the storage.

func (PeerStorage) Assign

func (s PeerStorage) Assign(ctx context.Context, key string, value storage.Peer) (rerr error)

Assign adds given peer to the storage and associate it to the given key.

func (PeerStorage) Find

Find finds peer using given key.

func (PeerStorage) Iterate

Iterate creates and returns new PeerIterator.

func (PeerStorage) Resolve

func (s PeerStorage) Resolve(ctx context.Context, key string) (storage.Peer, error)

Resolve finds peer using associated key.

type SessionStorage

type SessionStorage struct {
	kv.Session
}

SessionStorage is a MTProto session Redis storage.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/go-faster/errors"
	redisclient "github.com/redis/go-redis/v9"

	"github.com/gotd/td/telegram"

	"github.com/gotd/contrib/redis"
)

func redisStorage(ctx context.Context) error {
	redisClient := redisclient.NewClient(&redisclient.Options{})
	storage := redis.NewSessionStorage(redisClient, "session")

	client, err := telegram.ClientFromEnvironment(telegram.Options{
		SessionStorage: storage,
	})
	if err != nil {
		return errors.Errorf("create client: %w", err)
	}

	return client.Run(ctx, func(ctx context.Context) error {
		// Force redis to flush DB.
		// It may be necessary to be sure that session will be saved to the disk.
		if err := redisClient.FlushDBAsync(ctx).Err(); err != nil {
			return errors.Errorf("flush: %w", err)
		}

		_, err := client.Auth().Bot(ctx, os.Getenv("BOT_TOKEN"))
		return err
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := redisStorage(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(1)
	}
}

func NewSessionStorage

func NewSessionStorage(client *redis.Client, key string) SessionStorage

NewSessionStorage creates new SessionStorage.

type StateStorage

type StateStorage struct {
	// contains filtered or unexported fields
}

StateStorage implements updates.StateStorage using Redis.

func NewRedisStateStorage

func NewRedisStateStorage(client *redis.Client) *StateStorage

NewRedisStateStorage creates a new Redis-based state storage.

func (*StateStorage) ForEachChannels

func (s *StateStorage) ForEachChannels(ctx context.Context, userID int64, f func(ctx context.Context, channelID int64, pts int) error) error

ForEachChannels iterates over all channels for a user

func (*StateStorage) GetChannelPts

func (s *StateStorage) GetChannelPts(ctx context.Context, userID, channelID int64) (int, bool, error)

GetChannelPts retrieves the PTS for a specific channel

func (*StateStorage) GetState

func (s *StateStorage) GetState(ctx context.Context, userID int64) (updates.State, bool, error)

GetState loads the global state for a user.

func (*StateStorage) SetChannelPts

func (s *StateStorage) SetChannelPts(ctx context.Context, userID, channelID int64, pts int) error

SetChannelPts updates the PTS for a specific channel

func (*StateStorage) SetDate

func (s *StateStorage) SetDate(ctx context.Context, userID int64, date int) error

SetDate updates Date

func (*StateStorage) SetDateSeq

func (s *StateStorage) SetDateSeq(ctx context.Context, userID int64, date, seq int) error

SetDateSeq updates both date and seq

func (*StateStorage) SetPts

func (s *StateStorage) SetPts(ctx context.Context, userID int64, pts int) error

SetPts updates PTS

func (*StateStorage) SetQts

func (s *StateStorage) SetQts(ctx context.Context, userID int64, qts int) error

SetQts updates QTS

func (*StateStorage) SetSeq

func (s *StateStorage) SetSeq(ctx context.Context, userID int64, seq int) error

SetSeq updates Seq

func (*StateStorage) SetState

func (s *StateStorage) SetState(ctx context.Context, userID int64, state updates.State) error

SetState sets all global state fields.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL