Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to delete a cache entry #7016

Merged
merged 8 commits into from
Dec 12, 2022
Merged

Adds ability to delete a cache entry #7016

merged 8 commits into from
Dec 12, 2022

Conversation

cannikin
Copy link
Member

@cannikin cannikin commented Dec 2, 2022

This adds the ability to delete a cache entry by its key. This will allow you to be a little more lax in your cache key names if you know you have circumstances where you bust the cache explicitly so that it can be re-built.

Consider an example where you are caching a post by its id. You currently have no way to bust the cache when the post itself changes in some way. But if you can manually delete the cache entry when updating or deleting a post, you are good to go:

import { cache, deleteCacheKey } from 'src/lib/cache'

const post = ({ id }) => {
  return cache(`post-${id}`, () => {
    return db.post.findUnique({ where: { id } })
  })
})

const updatePost = async ({ id, input }) => {
  await deleteCacheKey(`post-${id}`)
  return db.post.update({ where: { id }, data: { input } })
})

Of course this assumes that this service is the only place where a post is updated. If not, you either need to make duplicate deleteCacheKey() calls, or come up with a key that automatically busts the cache on a post change (like incorporating the updatedAt timestamp).

The deleteCacheKey() function will return true if the key was found and deleted, otherwise it returns false.

Thanks for the suggestion @dthyresson!

Closes #7003

@cannikin cannikin self-assigned this Dec 2, 2022
@cannikin cannikin added release:feature This PR introduces a new feature topic/services labels Dec 2, 2022
@@ -185,8 +185,28 @@ export const createCache = (
return cache(latestCacheKey, () => model.findMany(conditions), rest)
}

const deleteCacheKey = async (key: CacheKey) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since both libs called the function delete (and del) I figured it was best to stay with that nomenclature. I know we talk about "invalidating the cache" but I wonder if that's the proper term to use for what we're doing...invalidation sounds like it's still around, it's just not "valid" any more. Whereas memcached and Redis really seem to delete it from memory and it's no longer available...

@cannikin cannikin merged commit b98b2a7 into main Dec 12, 2022
@cannikin cannikin deleted the rc-service-cache-delete branch December 12, 2022 20:57
@redwoodjs-bot redwoodjs-bot bot added this to the next-release milestone Dec 12, 2022
github-actions bot pushed a commit that referenced this pull request Dec 12, 2022
* Adds deleteCacheKey() to exported cache functions

* Reorg tests

* Remove some comments

* Update types, simplify Redis return on del()

* Fix double not on promise

* Add info block about using a hash of columns in the key
jtoar pushed a commit that referenced this pull request Dec 12, 2022
* Adds deleteCacheKey() to exported cache functions

* Reorg tests

* Remove some comments

* Update types, simplify Redis return on del()

* Fix double not on promise

* Add info block about using a hash of columns in the key
dac09 added a commit that referenced this pull request Dec 13, 2022
….com:redwoodjs/redwood into feat/dc-kc-decoupled-auth-setup-improvements

* 'feat/dc-kc-decoupled-auth-setup-improvements' of github.com:redwoodjs/redwood:
  chore(deps): update dependency nx to v15.3.2 (#7114)
  chore(deps): update dependency redis to v4.5.1 (#7115)
  fix: add missing deps to cli helpers (#7117)
  Adds ability to delete a cache entry (#7016)
  Label override flags for dbAuth generator (#6440)
  fix(deps): update dependency systeminformation to v5.16.6 (#7108)
  feat: Generator rollbacks  (#6947)
  fix(deps): update dependency @types/node to v16.18.8 (#7107)
  chore(deps): update dependency supertokens-node to v12.1.3 (#7105)
@jtoar jtoar modified the milestones: next-release, v3.8.0 Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature This PR introduces a new feature topic/services
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[RFC]: Service Caching: provide an interface for invalidating an existing key
3 participants