feat(migrations): Harden task audit tracking and optimize article templates
- Added migration to harden task audit tracking by modifying audit_logs and related tables. - Introduced operator_id to several tables for better tracking of actions. - Updated article_templates with new prompt templates for various article types, enhancing content generation. - Created prompt_rules and schedule_tasks tables to manage content generation rules and scheduling. - Added foreign key constraints to articles for better data integrity.
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrNotFound = errors.New("cache: key not found")
|
||||
|
||||
type Cache interface {
|
||||
Get(ctx context.Context, key string) ([]byte, error)
|
||||
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
|
||||
Delete(ctx context.Context, key string) error
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package cache
|
||||
|
||||
import goredis "github.com/redis/go-redis/v9"
|
||||
|
||||
func New(driver string, rdb *goredis.Client) Cache {
|
||||
switch driver {
|
||||
case "redis":
|
||||
return newRedisCache(rdb)
|
||||
default:
|
||||
return newMemoryCache()
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type memoryEntry struct {
|
||||
data []byte
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
type memoryCache struct {
|
||||
mu sync.RWMutex
|
||||
entries map[string]memoryEntry
|
||||
}
|
||||
|
||||
func newMemoryCache() Cache {
|
||||
c := &memoryCache{entries: make(map[string]memoryEntry)}
|
||||
go c.evictLoop()
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *memoryCache) Get(_ context.Context, key string) ([]byte, error) {
|
||||
c.mu.RLock()
|
||||
entry, ok := c.entries[key]
|
||||
c.mu.RUnlock()
|
||||
|
||||
if !ok || time.Now().After(entry.expiresAt) {
|
||||
if ok {
|
||||
c.mu.Lock()
|
||||
delete(c.entries, key)
|
||||
c.mu.Unlock()
|
||||
}
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
dst := make([]byte, len(entry.data))
|
||||
copy(dst, entry.data)
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
func (c *memoryCache) Set(_ context.Context, key string, value []byte, ttl time.Duration) error {
|
||||
dst := make([]byte, len(value))
|
||||
copy(dst, value)
|
||||
|
||||
c.mu.Lock()
|
||||
c.entries[key] = memoryEntry{data: dst, expiresAt: time.Now().Add(ttl)}
|
||||
c.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *memoryCache) Delete(_ context.Context, key string) error {
|
||||
c.mu.Lock()
|
||||
delete(c.entries, key)
|
||||
c.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *memoryCache) evictLoop() {
|
||||
ticker := time.NewTicker(60 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
now := time.Now()
|
||||
c.mu.Lock()
|
||||
for k, v := range c.entries {
|
||||
if now.After(v.expiresAt) {
|
||||
delete(c.entries, k)
|
||||
}
|
||||
}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
goredis "github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type redisCache struct {
|
||||
client *goredis.Client
|
||||
}
|
||||
|
||||
func newRedisCache(client *goredis.Client) Cache {
|
||||
return &redisCache{client: client}
|
||||
}
|
||||
|
||||
func (c *redisCache) Get(ctx context.Context, key string) ([]byte, error) {
|
||||
val, err := c.client.Get(ctx, key).Bytes()
|
||||
if errors.Is(err, goredis.Nil) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return val, err
|
||||
}
|
||||
|
||||
func (c *redisCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error {
|
||||
return c.client.Set(ctx, key, value, ttl).Err()
|
||||
}
|
||||
|
||||
func (c *redisCache) Delete(ctx context.Context, key string) error {
|
||||
return c.client.Del(ctx, key).Err()
|
||||
}
|
||||
Reference in New Issue
Block a user