prompt_payload_store.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package securityaudit
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "github.com/redis/go-redis/v9"
  8. )
  9. type PayloadStore interface {
  10. Set(ctx context.Context, jobID int64, scanText string, ttl time.Duration) error
  11. Get(ctx context.Context, jobID int64) (string, error)
  12. Delete(ctx context.Context, jobID int64) error
  13. Ping(ctx context.Context) error
  14. }
  15. type RedisPayloadStore struct {
  16. client *redis.Client
  17. }
  18. func NewRedisPayloadStore(client *redis.Client) *RedisPayloadStore {
  19. return &RedisPayloadStore{client: client}
  20. }
  21. func (s *RedisPayloadStore) Set(ctx context.Context, jobID int64, scanText string, ttl time.Duration) error {
  22. if s == nil || s.client == nil {
  23. return fmt.Errorf("prompt audit payload store unavailable")
  24. }
  25. if jobID <= 0 || scanText == "" {
  26. return fmt.Errorf("prompt audit payload input invalid")
  27. }
  28. if ttl <= 0 || ttl > DefaultPayloadTTL {
  29. ttl = DefaultPayloadTTL
  30. }
  31. return s.client.Set(ctx, payloadKey(jobID), scanText, ttl).Err()
  32. }
  33. func (s *RedisPayloadStore) Get(ctx context.Context, jobID int64) (string, error) {
  34. if s == nil || s.client == nil {
  35. return "", fmt.Errorf("prompt audit payload store unavailable")
  36. }
  37. return s.client.Get(ctx, payloadKey(jobID)).Result()
  38. }
  39. func (s *RedisPayloadStore) Delete(ctx context.Context, jobID int64) error {
  40. if s == nil || s.client == nil {
  41. return fmt.Errorf("prompt audit payload store unavailable")
  42. }
  43. return s.client.Del(ctx, payloadKey(jobID)).Err()
  44. }
  45. func (s *RedisPayloadStore) Ping(ctx context.Context) error {
  46. if s == nil || s.client == nil {
  47. return fmt.Errorf("prompt audit payload store unavailable")
  48. }
  49. return s.client.Ping(ctx).Err()
  50. }
  51. func payloadKey(jobID int64) string {
  52. return PayloadKeyPrefix + strconv.FormatInt(jobID, 10)
  53. }