villus icon indicating copy to clipboard operation
villus copied to clipboard

Manually setting cache

Open betweenbrain opened this issue 1 year ago • 7 comments

Hello,

We are looking to achieve something very similar to issue #29. In reviewing https://villus.logaretm.com/plugins/cache/#clearing-cache, it seems like one should be able to call cachePlugin.setCacheResult(...), but my initial attempts following the example throws TypeError: j.setCacheResult is not a function. Is this expected?

betweenbrain avatar Apr 17 '23 20:04 betweenbrain

Sorry for the delay, just got around to this. The plugin doesn't exposesetCacheResult, only the clear method.

But I guess we could expose it, will try to tackle it this week. PR is welcome as well, thanks for reporting this.

logaretm avatar May 09 '23 21:05 logaretm

Thanks for the response! And, now I owe you an apology for the delay 😆

Would exposing it be as simple as adding something like cachPlugin.setCacheResult = setCacheResult(operation, result); to https://github.com/logaretm/villus/blob/main/packages/villus/src/cache.ts#L80?

betweenbrain avatar May 24 '23 17:05 betweenbrain

Hello, it is the other way around. Sorry, I didn't find the time to work on it, busy with vee-validate at the moment.

It is as simple as you suggested. But how would you set the cache yourself? Would you pass an object containing the query and variables? Because it relies on key being provided, so it might be worth regenerating the key before then.

cachPlugin.setCacheResult = function (operation, result) {
  // getQueryKey imported from utils
  const key = operation.key || getQueryKey(operation);
 setCacheResult({ key, ...operation }, result);
}

logaretm avatar May 25 '23 13:05 logaretm

Hello, thanks again for the response. No apologies are needed!

But how would you set the cache yourself? Would you pass an object containing the query and variables?

Yes, that's exactly how I was thinking we would do it.

Is there anything that I can do to help move this forward?

betweenbrain avatar May 30 '23 13:05 betweenbrain

Is there anything that I can do to help move this forward?

Feel free to PR it if you have the time, I know it's a 1 liner but I usually do open-source on weekends and whenever I can sneak in work days. I will take it on by the weekend if you won't have the time.

logaretm avatar May 30 '23 14:05 logaretm

Great! PR submitted at https://github.com/logaretm/villus/pull/198. I hope that helps.

betweenbrain avatar Jun 13 '23 14:06 betweenbrain

@logaretm do you have a chance to review the above PR? Thanks!

betweenbrain avatar Oct 23 '23 19:10 betweenbrain