0

I have this TTL expiration checker that takes creationDate string & ttl int (seconds):

package utils

import ...

func IsExpired(creationDate string, ttl int) (bool, error) {
    createdAt, err := time.Parse("2006-01-02 15:04:05", creationDate)

    if err != nil {
        return false, err
    }

    expirationTime := createdAt.Add(time.Duration(ttl) * time.Second)
    currentTime := time.Now()

    return currentTime.After(expirationTime), nil
}

When I pass this JSON dataset (parsed of course):

{"hello":{"value":"world","created_at":"2023-06-08 18:08:22","ttl":2}}

It returns false for the first 2 seconds which I expect. But then even after 2 seconds, it returns false? I looked for a lot of solutions but still all of them give me the same snippet which gives always returns false.

Here is where I am using it if needed:

func CleanDB() {
    ticker := time.NewTicker(5 * time.Second)

    for range ticker.C {
        mapJsonContent, err := readFromCache()

        if err != nil {
            fmt.Println("Database cleaner: cleaning iteration failed.")
            return
        }

        for key, data := range mapJsonContent {
            if data.CreatedAt == "" {
                continue
            }

            expired, err := utils.IsExpired(data.CreatedAt, data.TTL)

            if err != nil {
                fmt.Printf("Database cleaner: Failed to parse `map[%s].CreatedAt` as datetime\n", key)
                continue
            }

            // Always prints "next false"
            fmt.Println("next ", expired)

            if expired {
                // NEVER prints
                fmt.Println("expired: ", key)
                DeleteValue(key)
            }
        }
    }
}

The CleanDB is run as a go routine in the main.go file.

Can anyone help ?

Dev-Siri
  • 592
  • 5
  • 22
  • The timestamps have no timezone information, are they local time or UTC? – JimB Jun 08 '23 at 13:09
  • @JimB They are local time. – Dev-Siri Jun 08 '23 at 13:12
  • This code is only deleting data if it's expired, and not updating the cache to remove the expired data. Am I correct? If yes, then this means that even if some data is expired, it will still remain in the cache and continue to be checked in further iterations of the loop. – Jishan Shaikh Jun 08 '23 at 13:12
  • @Dev-Siri, then you need to parse them in the local location, not UTC – JimB Jun 08 '23 at 13:12
  • @JishanShaikh the DeleteValue( ) func under-the-hood does delete it from cache as well as from memory. – Dev-Siri Jun 08 '23 at 13:14
  • @Dev-Siri I am not sure about that. Maybe try exploring deleting manually, something like this: `delete(mapJsonContent, key) err := writeToCache(mapJsonContent)`, just after `DeleteValue(key)` – Jishan Shaikh Jun 08 '23 at 13:20
  • @JishanShaikh My issue has been fixed as JimB told I did not provide any time zone info. So it was being stored as Local time. I needed to use `time.ParseInLocation( )` with time.Local. Now it works fine – Dev-Siri Jun 08 '23 at 13:22

0 Answers0