File: redis.go

package info (click to toggle)
golang-github-tideland-golib 4.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,144 kB
  • sloc: makefile: 4
file content (114 lines) | stat: -rw-r--r-- 2,645 bytes parent folder | download | duplicates (2)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Tideland Go Library - Redis Client
//
// Copyright (C) 2009-2017 Frank Mueller / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.

package redis

//--------------------
// IMPORTS
//--------------------

import (
	"fmt"
	"sync"
	"time"
)

//--------------------
// DATABASE
//--------------------

// Database provides access to a Redis database.
type Database struct {
	mux        sync.Mutex
	address    string
	network    string
	timeout    time.Duration
	index      int
	password   string
	poolsize   int
	logging    bool
	monitoring bool
	pool       *pool
}

// Open opens the connection to a Redis database based on the
// passed options.
func Open(options ...Option) (*Database, error) {
	db := &Database{
		address:    defaultSocket,
		network:    defaultNetwork,
		timeout:    defaultTimeout,
		index:      defaultIndex,
		password:   defaultPassword,
		poolsize:   defaultPoolSize,
		logging:    defaultLogging,
		monitoring: defaultMonitoring,
	}
	for _, option := range options {
		if err := option(db); err != nil {
			return nil, err
		}
	}
	db.pool = newPool(db)
	return db, nil
}

// Options returns the configuration of the database.
func (db *Database) Options() Options {
	db.mux.Lock()
	defer db.mux.Unlock()
	return Options{
		Address:    db.address,
		Network:    db.network,
		Timeout:    db.timeout,
		Index:      db.index,
		Password:   db.password,
		PoolSize:   db.poolsize,
		Logging:    db.logging,
		Monitoring: db.monitoring,
	}
}

// Connection returns one of the pooled connections to the Redis
// server. It has to be returned with conn.Return() after usage.
func (db *Database) Connection() (*Connection, error) {
	db.mux.Lock()
	defer db.mux.Unlock()
	return newConnection(db)
}

// Pipeline returns one of the pooled connections to the Redis
// server running in pipeline mode. Calling ppl.Collect()
// collects all results and returns the connection.
func (db *Database) Pipeline() (*Pipeline, error) {
	db.mux.Lock()
	defer db.mux.Unlock()
	return newPipeline(db)
}

// Subscription returns a subscription with a connection to the
// Redis server. It has to be closed with sub.Close() after usage.
func (db *Database) Subscription() (*Subscription, error) {
	db.mux.Lock()
	defer db.mux.Unlock()
	return newSubscription(db)
}

// Close closes the database client.
func (db *Database) Close() error {
	db.mux.Lock()
	defer db.mux.Unlock()
	return db.pool.close()
}

// String implements the Stringer interface and returns address
// plus index.
func (db *Database) String() string {
	return fmt.Sprintf("%s:%d", db.address, db.index)
}

// EOF