Comments (9)
I have written the following:
updateCache
:: (Show a, Eq (req a), Hashable (req a), Show (req a), Typeable (req a))
=> req a
-> a
-> GenHaxl u ()
updateCache request result = GenHaxl $ \env -> do
cache <- readIORef (cacheRef env)
case DC.lookup request cache of
Nothing -> do
ivar <- newFullIVar $ Ok result
writeIORef (cacheRef env) $! DC.insert request ivar cache
done (Ok ())
Just iv@(IVar cr) -> do
e <- readIORef cr
case e of
IVarFull (ThrowIO ex) ->
return $ Throw ex
IVarFull (ThrowHaxl ex) ->
return $ Throw ex
IVarFull (Ok _) -> do
writeIORef cr $! IVarFull (Ok result)
done (Ok ())
IVarEmpty JobNil -> do
writeIORef cr $! IVarFull (Ok result)
done (Ok ())
IVarEmpty _ ->
return $ Blocked iv (Cont $ updateCache request result)
However, while I think I understand the basics of haxl's architecture, I can't really determine whether it's correct or if it'll break everything.
EDIT:
This does indeed break everything. Starting an async fetch and then modifying the cache this way crashes because it's not possible to add a job to fetch something that has already been fetched/is in cache.
I've made some other attempts but so far I've been unsuccessful.
from haxl.
Having cache without ability to update, modify, clear or invalidate it sounds like a serious problem.
from haxl.
@saevarb this is a completely reasonable thing to want to do. Indeed, I've wanted to do similar things myself on occasion, but never got around to implementing it.
To answer your specific points:
-
We don't allow the cache to be modified, because we would lose the property that evaluating a given computation twice yields the same result, which is what we rely on for
memo
and the other memoization primitives to be valid. However, it's reasonable to have an operation likecacheRequest
which adds an entry to the cache if it isn't already there - this will require an additionalEq
constraint to compare results. -
Yes absolutely. I think it would be great to have better support for this. Adding entries to the cache manually (as in your example) is one way, but could be inefficient in general. What we probably need is a more general way to look for a cached result, but I haven't tried to do this, it's not clear to me yet what the right abstractions are. It would definitely be interesting to explore this.
from haxl.
@simonmar wouldn't the cache invalidation itself require an additional function to evaluate to see if / when the proper action can take place? As part of your traversal you can take in something like a time to represent the state which can invalidate. A new time means new data and will not violate RT.
from haxl.
@somethingconcon to avoid confusion, could you give the type(s) of the operations you're suggesting?
from haxl.
@simonmar Sorry, I'm not 100% sure what I should provide as an example. Could you explain what your expectation would be so I can provide a proper example?
from haxl.
@somethingconcon Well, I didn't fully understand your comment, so I think making it concrete with some actual code would help. e.g. I'm not sure what the "additional function" is, not sure what the traversal you mention is, not sure what would need to take a time or why. Basically could you explain in more detail please :)
from haxl.
Oh, yes. My apologies on not being clear. For the sake of brevity, it looks like I cut out all important detail. I will try to provide a code example after the workday. Thanks! :)
from haxl.
@saevarb prepareMemo and runMemo sound like they would help with one and possibly two.
If you’d don’t need that granularity, then cachedComputation is a service function that will handle such details behind the implementation.
The items in a cached computation aren’t stored across rounds however, but this would mean fetches in the same round for colliding signatures would be handled as you’re going for.
uncachedRequest is the machinery for ‘updating’ as far as I can tell.
The signature for the request will be what determines which slot in the result map your value is assigned to, so depending on what parameters you use for your signature definitions in your Hashable instance it should transparently update the value for subsequent fetches.
from haxl.
Related Issues (20)
- 2.1.*.0 tags and Hackage release HOT 1
- Building Error HOT 1
- Plans for a monad transformer interface? HOT 1
- New version to Hackage for GHC 8.8 and beyond HOT 3
- DataCacheTest fails to build with executable profiling enabled
- Release version compatible with aeson-2 and GHC 9 HOT 2
- Adding MonadMask instance HOT 1
- Rename Show1 to ShowP HOT 2
- This package is not licensed under BSD3 HOT 2
- haxl failing to compile with ghc-8.2 rc2 HOT 6
- Memory consumption depending on type annotation HOT 3
- Non-deterministic failure in collectsdata test
- Haxl 2 release on Hackage HOT 3
- Broken Hackage links HOT 1
- 2.0.0.0 tag is missing. HOT 1
- Don't rely on `Show` to be valid Haskell code HOT 4
- Inaccurate lower bound on `base`
- tight upper bounds for ghc-8.6.1 HOT 1
- runHaxl stuck HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from haxl.