better-buddy-share-backend
    Preparing search index...

    Base abstraction for DAOs that use a caching layer.

    Provides helper methods to interact with CacheService and maps underlying cache errors into DaoError subclasses.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    cacheService: CacheService

    Instance of the CacheService used to perform cache operations.

    Methods

    • Get a value from cache by key.

      Returns null when the key does not exist.

      Type Parameters

      • T

        Expected type of the cached value.

      Parameters

      • key: string

        Cache key to read.

      Returns Promise<T | null>

      The value stored under the key or null if not present.

      DaoError when a cache-related error occurs.

    • Get multiple values from cache by an array of keys.

      If the underlying CacheService does not provide a batch get, this method falls back to parallel single gets.

      Type Parameters

      • T

        Expected type of each cached value.

      Parameters

      • keys: string[]

        Array of cache keys to read.

      Returns Promise<(T | null)[]>

      An array containing the values or nulls for missing keys.

      DaoError when a cache-related error occurs.

    • Set a value in cache with optional TTL.

      Type Parameters

      • T

        Type of the value to store.

      Parameters

      • key: string

        Cache key to set.

      • value: T

        Value to store.

      • OptionalttlSeconds: number

        Optional time-to-live in seconds.

      Returns Promise<void>

      DaoError when a cache-related error occurs.

    • Removes a user from Redis cache

      • Removes user entry from cache under key user:${id}
      • Uses CacheService.del() for consistent error handling
      • Logs information about user removal

      Parameters

      • id: number

        User ID to remove from cache

      Returns Promise<void>

      Error when Redis deletion problem occurs

      await userCache.deleteUser(123);
      console.log('User removed from cache');
    • Searches for a user in Redis cache by email address

      • First fetches user ID from key user:email:${email}
      • Then calls findById() to fetch complete user data
      • Logs cache hit/miss information for email searches
      • Uses two-step lookup: email -> user_id -> user data

      Parameters

      • email: string

        User email address to search for

      Returns Promise<User | null>

      Promise resolving to user object or null if not found

      Error when Redis connection problem occurs

      const user = await userCache.findByEmail('user@example.com');
      if (user) {
      console.log(`Found user with ID: ${user.user_id}`);
      }
    • Searches for a user in Redis cache by ID

      • Fetches user from cache under key user:${id}
      • Automatically converts date strings to Date objects (created_at, ban_expires_at)
      • Logs cache hit/miss information for debugging purposes
      • Uses CacheService for consistent error handling

      Parameters

      • id: number

        User ID to search for

      Returns Promise<User | null>

      Promise resolving to user object or null if not found

      Error when Redis connection problem occurs

      const user = await userCache.findById(123);
      if (user) {
      console.log(`Found user: ${user.username}`);
      }
    • Creates or updates a user in Redis cache

      • Checks if user already exists in cache using findById()
      • If exists and data is identical (comparison via lodash.isEqual), skips update
      • If exists but data differs, removes old entry and creates new one
      • Saves user under key user:${id} and creates mapping user:email:${email} -> user_id
      • Sets cache expiration to 1 hour (3600 seconds) for both keys
      • Uses Promise.all() for parallel Redis operations

      Parameters

      • user: User

        User object to save in cache

      Returns Promise<User>

      Promise resolving to the user object (same as passed)

      Error when Redis write problem occurs

      const userData = {
      user_id: 123,
      username: 'john_doe',
      email: 'john@example.com',
      created_at: new Date()
      };

      const cachedUser = await userCache.upsertUser(userData);
      console.log('User cached successfully:', cachedUser.username);