Giter Club home page Giter Club logo

Comments (1)

dalssoft avatar dalssoft commented on June 28, 2024

Here are some thoughts.

Before starting going deep into the code, I would like to understand the use case. There are two scenarios that I can think of where memcached could be used:

no database, just memcached

In this case, memcached is the main repository, no other database is used. The approach you are presenting is good for this scenario. However, I can't think of a real use case for this scenario, where all the data is stored in a volatile memory, with no persistence. Probably there are some, but it is more a exception. In this case would be worth the effort?

database + memcached

Here, the memcached is used as a cache for the database.

In this case, the glue should be a cache adapter (or something like that [1]), not a repository.

The adapter wouldn't need a data mapper to save the entity, the serialization is straightforward (JSON.stringify) and could use the metadata to set the key (entity IDs).

To fetch the entity, the adapter would need a data mapper to deserialize the a valid entity since it would not interact with the repository. And fallback to the repository if the entity is not in the cache.

The adapter should be able to work with any repository. This can be a challenge, since the adapter would need to know the the interface of different repositories (herbs2knex, herbs2mongo, etc).

// very simplified example
const User =
        entity('User', {
          id: id(String),
          nickname: field(String),
          password: field(String)
        })

const user = new User({ id: '1', nickname: 'john', password: '123' })

const userRepo = new UserRepo()
const userCache = new UserCache(userRepo)

await userCache.insert(user, { ttl: 1000 })
const userFromCache = await userCache.getById('1')

[1] If not a adapter, maybe a chain of responsibility of repositories, where the first handler is the cache and the second is the repository.

Does it make sense?

from herbs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.