-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate middleware changes with internalization of SqlBackend
- Loading branch information
Showing
12 changed files
with
132 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 16 additions & 47 deletions
63
persistent/Database/Persist/SqlBackend/Internal/StatementCache.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,23 @@ | ||
module Database.Persist.SqlBackend.Internal.StatementCache | ||
( StatementCache(..) | ||
, InternalStatementCache | ||
, makeSimpleStatementCache | ||
, internalizeStatementCache | ||
) where | ||
module Database.Persist.SqlBackend.Internal.StatementCache where | ||
|
||
import Data.Foldable | ||
import Data.IORef | ||
import qualified Data.Map as Map | ||
import Data.Text (Text) | ||
import Database.Persist.SqlBackend.Internal.Statement | ||
|
||
class StatementCache c where | ||
statementCacheLookup :: c -> Text -> IO (Maybe Statement) | ||
statementCacheInsert :: c -> Text -> Statement -> IO () | ||
statementCacheClear :: c -> IO () | ||
statementCacheSize :: c -> IO Int | ||
|
||
data InternalStatementCache = InternalStatementCache | ||
{ _statementCacheLookup :: Text -> IO (Maybe Statement) | ||
, _statementCacheInsert :: Text -> Statement -> IO () | ||
, _statementCacheClear :: IO () | ||
, _statementCacheSize :: IO Int | ||
-- | A statement cache used to lookup statements that have already been prepared | ||
-- for a given query. | ||
-- | ||
-- @since 2.13.0 | ||
data StatementCache = StatementCache | ||
{ statementCacheLookup :: StatementCacheKey -> IO (Maybe Statement) | ||
, statementCacheInsert :: StatementCacheKey -> Statement -> IO () | ||
, statementCacheClear :: IO () | ||
, statementCacheSize :: IO Int | ||
} | ||
|
||
instance StatementCache InternalStatementCache where | ||
statementCacheLookup = _statementCacheLookup | ||
statementCacheInsert = _statementCacheInsert | ||
statementCacheClear = _statementCacheClear | ||
statementCacheSize = _statementCacheSize | ||
|
||
|
||
internalizeStatementCache :: StatementCache c => c -> InternalStatementCache | ||
internalizeStatementCache c = InternalStatementCache | ||
{ _statementCacheLookup = statementCacheLookup c | ||
, _statementCacheInsert = statementCacheInsert c | ||
, _statementCacheClear = statementCacheClear c | ||
, _statementCacheSize = statementCacheSize c | ||
} | ||
|
||
makeSimpleStatementCache :: IO InternalStatementCache | ||
makeSimpleStatementCache = do | ||
stmtMap <- newIORef Map.empty | ||
pure $ InternalStatementCache | ||
{ _statementCacheLookup = \sql -> Map.lookup sql <$> readIORef stmtMap | ||
, _statementCacheInsert = \sql stmt -> | ||
modifyIORef' stmtMap (Map.insert sql stmt) | ||
, _statementCacheClear = do | ||
oldStatements <- atomicModifyIORef' stmtMap (\oldStatements -> (Map.empty, oldStatements)) | ||
traverse_ stmtFinalize oldStatements | ||
, _statementCacheSize = Map.size <$> readIORef stmtMap | ||
} | ||
newtype StatementCacheKey = StatementCacheKey { cacheKey :: Text } | ||
-- Wrapping around this to allow for more efficient keying mechanisms | ||
-- in the future, perhaps. | ||
|
||
-- | Construct a `StatementCacheKey` from a raw SQL query. | ||
mkCacheKeyFromQuery :: Text -> StatementCacheKey | ||
mkCacheKeyFromQuery = StatementCacheKey |
Oops, something went wrong.