help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
go-rest-api
/
internal
/
cache
RSK World
go-rest-api
Go REST API - Enterprise-grade REST API with JWT Authentication + PostgreSQL + Redis Caching + Docker + Comprehensive Testing + Educational Design
cache
  • cache.go2.3 KB
cache.go
internal/cache/cache.go
Raw Download
Find: Go to:
/*
* Author: RSK World
* Email: help@rskworld.in / support@rskworld.in
* Website: https://rskworld.in/contact.php
* Year: 2026
*/

package cache

import (
	"context"
	"encoding/json"
	"fmt"
	"time"

	"github.com/go-redis/redis/v8"
	"github.com/rskworld/go-rest-api/internal/config"
)

type Cache struct {
	client *redis.Client
	ctx    context.Context
}

var RedisClient *Cache

func InitCache(cfg *config.Config) error {
	if !cfg.CacheEnabled {
		return nil
	}

	rdb := redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%s", cfg.RedisHost, cfg.RedisPort),
		Password: cfg.RedisPassword,
		DB:       cfg.RedisDB,
	})

	ctx := context.Background()

	// Test connection
	_, err := rdb.Ping(ctx).Result()
	if err != nil {
		return fmt.Errorf("failed to connect to Redis: %v", err)
	}

	RedisClient = &Cache{
		client: rdb,
		ctx:    ctx,
	}

	return nil
}

func (c *Cache) Set(key string, value interface{}, expiration time.Duration) error {
	if c == nil {
		return nil // Cache disabled
	}

	jsonData, err := json.Marshal(value)
	if err != nil {
		return err
	}

	return c.client.Set(c.ctx, key, jsonData, expiration).Err()
}

func (c *Cache) Get(key string, dest interface{}) error {
	if c == nil {
		return redis.Nil // Cache disabled
	}

	val, err := c.client.Get(c.ctx, key).Result()
	if err != nil {
		return err
	}

	return json.Unmarshal([]byte(val), dest)
}

func (c *Cache) Delete(key string) error {
	if c == nil {
		return nil // Cache disabled
	}

	return c.client.Del(c.ctx, key).Err()
}

func (c *Cache) Exists(key string) bool {
	if c == nil {
		return false // Cache disabled
	}

	count, err := c.client.Exists(c.ctx, key).Result()
	return err == nil && count > 0
}

func (c *Cache) FlushAll() error {
	if c == nil {
		return nil // Cache disabled
	}

	return c.client.FlushAll(c.ctx).Err()
}

// Cache keys
const (
	ProductKeyPrefix  = "product:"
	ProductsKey       = "products"
	CategoryKeyPrefix = "category:"
	CategoriesKey     = "categories"
	UserKeyPrefix     = "user:"
)

// Cache durations
const (
	ProductCacheDuration  = 10 * time.Minute
	CategoryCacheDuration = 15 * time.Minute
	UserCacheDuration     = 30 * time.Minute
	ListCacheDuration     = 5 * time.Minute
)
120 lines•2.3 KB
go

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer