sto cercando di utilizzare la funzione cached
per evitare che più query db in diversi widgets e gestori:Come utilizzare la cache di yesod per richiesta?
newtype CachedBobId key
= CachedBobId { unCachedBobId :: key }
deriving Typeable
getBob' :: Handler BobId
getBob' = do
uncle <- runInputGet $ ireq textField "bobsuncle"
(Entity bob _) <- runDB $ getBy404 $ UniqueBob uncle
return bob
getBob :: Handler BobId
getBob = do
a <- getBob'
let b = return $ CachedBobId a
c <- cached b
return $ unCachedBobId c
E in un widget da qualche parte:
renderDerp :: Widget
renderDerp = do
--these are used in the shakespeare files
lolBob <- handlerToWidget $ getBob
nutherBob <- handlerToWidget $ getBob
$(widgetFile "test")
Questo compila ma la query per ottenere la L'ID viene eseguito ancora più volte.
Cosa sto sbagliando? O c'è un modo migliore per ottenere bob solo una volta e usarlo in ogni gestore e widget?
Non ha veramente letto il codice all'interno della domanda (poiché ho avuto un problema con 'cache anche '). Qualcuno otterrà davvero 200 punti facili. – Cthulhu