#45 Golang - Caching in Go: How to Integrate Memcached

Поділитися
Вставка
  • Опубліковано 31 гру 2024

КОМЕНТАРІ • 16

  • @surajnirala9072
    @surajnirala9072 5 місяців тому +2

    One of the best channel for the golang...

    • @codeheim
      @codeheim  5 місяців тому

      Thank you. Please spread the word.

  • @slickmorty9845
    @slickmorty9845 5 місяців тому

    really great videos

    • @codeheim
      @codeheim  5 місяців тому

      Glad you like them!

  • @weihe-w2u
    @weihe-w2u 5 місяців тому +2

    May I ask a question? Why not do it this way?
    func CacheData(cacheKey string, ttl int32, callback func() []byte) []byte {
    item, err := mc.Get(cacheKey)
    var retValue []byte
    if err != nil {
    if !errors.Is(err, memcache.ErrCacheMiss) {
    logs.Error("memcache error: ", err)
    }
    retValue = callback()
    // Cache the result
    memcacheItem := &memcache.Item{
    Key: cacheKey,
    Value: retValue,
    Expiration: ttl,
    }
    if err := mc.Set(memcacheItem); err != nil {
    logs.Error("memcache error: ", err)
    }
    } else {
    retValue = item.Value
    }
    return retValue
    }

    • @codeheim
      @codeheim  5 місяців тому

      Yes, this is a great way.
      I started with an assumption that the function passed can have any kind of args.
      I think this would have been simpler.

    • @imjulianeral
      @imjulianeral 5 місяців тому

      Thanks! the error handling and types are way better like this!

    • @weihe-w2u
      @weihe-w2u 5 місяців тому

      @@codeheimThinks,
      learning from you.

  • @tomaszkotlarek3786
    @tomaszkotlarek3786 5 місяців тому

    What will happen if there will be many requests for the same item that is not in the cache? Is there any locking mechanism to generate the item only once for the first request so the rest of them will just wait? Or there should be some kind of warm up mechanism on memcache restart so every item is in the cache before any request occurs? And also cache updates on any edit to the stored item.

    • @codeheim
      @codeheim  5 місяців тому +1

      I think, even in case of burst of requests there would be a few requests that will be head of others. These requests will warm up the cache. In some specific scenarios we can choose to cache the results if we expect a lot of requests hitting the system

  • @marschax
    @marschax 5 місяців тому

    Nice Tutorial, but why you implement the cache function complex with a callback? You can simply pass the byte array as third parameter, or?

    • @codeheim
      @codeheim  5 місяців тому

      I used this mechanism so that we can pass any kind of function. You are right too.

  • @ragnadrok7
    @ragnadrok7 3 місяці тому

    I thought redis was more favored than memcached

    • @codeheim
      @codeheim  Місяць тому

      Yes, I too prefer Redis. But Memcached is popular too.

  • @hardikmaind9833
    @hardikmaind9833 4 місяці тому

    thanks