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

feat: expiring cache #22

Merged
merged 4 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
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
18 changes: 5 additions & 13 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,19 @@ import (
"time"

"github.com/gofiber/storage/sqlite3/v2"
"github.com/ochom/gutils/helpers"
)

var store *sqlite3.Storage

// Init creates a new cache instance
func Init(dbPath, tableName string) {
store = initCache(dbPath, tableName)
func init() {
database := helpers.GetEnv("CACHE_DB_PATH", "./fiber.sqlite3")
table := helpers.GetEnv("CACHE_TABLE_NAME", "fiber_storage")
store = initCache(database, table)
}

// initCache creates a new cache instance
func initCache(dbPath, tableName string) *sqlite3.Storage {
path := dbPath
if path == "" {
path = "./fiber.sqlite3"
}

name := tableName
if name == "" {
name = "fiber_storage"
}

return sqlite3.New(sqlite3.Config{
Database: dbPath,
Table: tableName,
Expand Down
2 changes: 0 additions & 2 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func TestSet(t *testing.T) {
cache.Init("", "")
type args struct {
key string
value []byte
Expand All @@ -32,7 +31,6 @@ func TestSet(t *testing.T) {
}

func TestGet(t *testing.T) {
cache.Init("", "")
type args struct {
key string
}
Expand Down
Binary file modified cache/fiber.sqlite3
Binary file not shown.
10 changes: 5 additions & 5 deletions helpers/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ func GenerateOTP(size int) string {
}

// HashPassword hashes a password
func HashPassword(password string) []byte {
func HashPassword(password string) string {
bsp, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return []byte{}
return ""
}

return bsp
return string(bsp)
}

// ComparePassword compares a password with a hash
func ComparePassword(hash []byte, password string) bool {
err := bcrypt.CompareHashAndPassword(hash, []byte(password))
func ComparePassword(hashedString string, password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hashedString), []byte(password))
return err == nil
}
2 changes: 1 addition & 1 deletion helpers/password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestGenerateOTP(t *testing.T) {

func TestPasswordHash(t *testing.T) {
hash := helpers.HashPassword("password")
if !helpers.ComparePassword([]byte(hash), "password") {
if !helpers.ComparePassword(hash, "password") {
t.Errorf("PasswordHash() = %v, want %v", hash, "password")
}
}
7 changes: 3 additions & 4 deletions pubsub/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@ func newConsumer(rabbitURL, queueName string) *Consumer {
}

// Consume consume messages from the channels
func Consume(queueName string, delayed bool, workerFunc func([]byte)) error {
func Consume(queueName string, workerFunc func([]byte)) error {
c := newConsumer(rabbitURL, queueName)
conn, ch, err := initQ(c.url)
if err != nil {
return fmt.Errorf("failed to initialize a connection: %s", err.Error())
}

defer ch.Close()
defer conn.Close()

if err := bind(ch, c.exchange, c.queue, delayed); err != nil {
return err
if err := initPubSub(ch, c.exchange, c.queue); err != nil {
return fmt.Errorf("failed to initialize a pubsub: %s", err.Error())
}

deliveries, err := ch.Consume(
Expand Down
4 changes: 1 addition & 3 deletions pubsub/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
func TestConsume(t *testing.T) {
type args struct {
queueName string
delayed bool
workerFunc func([]byte)
}
tests := []struct {
Expand All @@ -20,7 +19,6 @@ func TestConsume(t *testing.T) {
name: "test 1",
args: args{
queueName: "test",
delayed: true,
workerFunc: func(body []byte) {
fmt.Println(string(body))
},
Expand All @@ -32,7 +30,7 @@ func TestConsume(t *testing.T) {
t.Skip("Skipping test in non-local environment")
}
t.Run(tt.name, func(t *testing.T) {
if err := Consume(tt.args.queueName, tt.args.delayed, tt.args.workerFunc); (err != nil) != tt.wantErr {
if err := Consume(tt.args.queueName, tt.args.workerFunc); (err != nil) != tt.wantErr {
t.Errorf("Consume() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
8 changes: 4 additions & 4 deletions pubsub/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newPublisher(queueName string) *publisher {
}

// publish ...
func (p *publisher) publish(body []byte, delay time.Duration, delayed bool) error {
func (p *publisher) publish(body []byte, delay time.Duration) error {
conn, ch, err := initQ(p.url)
if err != nil {
return err
Expand All @@ -30,7 +30,7 @@ func (p *publisher) publish(body []byte, delay time.Duration, delayed bool) erro
defer ch.Close()
defer conn.Close()

if err := bind(ch, p.exchange, p.queue, delayed); err != nil {
if err := initPubSub(ch, p.exchange, p.queue); err != nil {
return err
}

Expand All @@ -56,11 +56,11 @@ func (p *publisher) publish(body []byte, delay time.Duration, delayed bool) erro
// PublishWithDelay ...
func PublishWithDelay(queueName string, body []byte, delay time.Duration) error {
p := newPublisher(fmt.Sprintf("%s-%s", queuePrefix, queueName))
return p.publish(body, delay, true)
return p.publish(body, delay)
}

// Publish ...
func Publish(queueName string, body []byte) error {
p := newPublisher(fmt.Sprintf("%s-%s", queuePrefix, queueName))
return p.publish(body, 0, false)
return p.publish(body, 0)
}
32 changes: 10 additions & 22 deletions pubsub/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ func initQ(url string) (*amqp.Connection, *amqp.Channel, error) {
}

// initPubSub ...
func initPubSub(ch *amqp.Channel, args amqp.Table, channelType, exchangeName, queueName string) error {
func initPubSub(ch *amqp.Channel, exchangeName, queueName string) error {
err := ch.ExchangeDeclare(
exchangeName, // name
channelType, // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
args, // arguments
exchangeName, // name
"x-delayed-message", // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
amqp.Table{
"x-delayed-type": "direct",
}, // arguments
)
if err != nil {
return fmt.Errorf("exchange Declare: %s", err.Error())
Expand Down Expand Up @@ -76,17 +78,3 @@ func initPubSub(ch *amqp.Channel, args amqp.Table, channelType, exchangeName, qu

return nil
}

// bind ...
func bind(ch *amqp.Channel, exchangeName, queueName string, delayed bool) error {
args := make(amqp.Table)
if delayed {
args["x-delayed-type"] = "direct"
channelType := "x-delayed-message"
return initPubSub(ch, args, channelType, exchangeName, queueName)
}

args["x-queue-mode"] = "lazy"
channelType := "direct"
return initPubSub(ch, args, channelType, exchangeName, queueName)
}