iand675 · GitHub

@iand675

parsonsmatt

setConnInsertManySql insertManySql' $
maybe id setConnRepsertManySql (upsertFunction repsertManySql serverVersion) $
mkSqlBackend MkSqlBackendArgs
modifyConnVault (Vault.insert underlyingConnectionKey conn) $ mkSqlBackend MkSqlBackendArgs

Comment on lines +371 to +373

Comment on lines +108 to +119

data SqlPoolHooks m backend = SqlPoolHooks
{ alterBackend :: backend -> m backend
-- ^ Alter the backend prior to executing any actions with it.
, runBefore :: backend -> Maybe IsolationLevel -> m ()
-- ^ Run this action immediately before the action is performed.
, runAfter :: backend -> Maybe IsolationLevel -> m ()
-- ^ Run this action immediately after the action is completed.
, runOnException :: backend -> Maybe IsolationLevel -> UE.SomeException -> m ()
-- ^ This action is performed when an exception is received. The
-- exception is provided as a convenience - it is rethrown once this
-- cleanup function is complete.
}
SqlBackend
, mkSqlBackend
, MkSqlBackendArgs(..)
, SqlBackendHooks(..)
-- ^ This function generates the SQL and values necessary for
-- performing an insert against the database.
, connStmtMap :: IORef (Map Text Statement)
, connStmtMap :: StatementCache

Comment on lines +2 to +9

module Database.Persist.SqlBackend.StatementCache
( StatementCache
, StatementCacheKey
, mkCacheKeyFromQuery
, MkStatementCache(..)
, mkSimpleStatementCache
, mkStatementCache
) where

Comment on lines +20 to +42

data MkStatementCache = MkStatementCache
{ statementCacheLookup :: StatementCacheKey -> IO (Maybe Statement)
-- ^ Retrieve a statement from the cache, or return nothing if it is not found.
--
-- @since 2.14.0
, statementCacheInsert :: StatementCacheKey -> Statement -> IO ()
-- ^ Put a new statement into the cache. An immediate lookup of
-- the statement MUST return the inserted statement for the given
-- cache key. Depending on the implementation, the statement cache MAY
-- choose to evict other statements from the cache within this function.
--
-- @since 2.14.0
, statementCacheClear :: IO ()
-- ^ Remove all statements from the cache. Implementations of this
-- should be sure to call `stmtFinalize` on all statements removed
-- from the cache.
--
-- @since 2.14.0
, statementCacheSize :: IO Int
-- ^ Get the current size of the cache.
--
-- @since 2.14.0
}

Collaborator

Suggested change

data MkStatementCache = MkStatementCache
{ statementCacheLookup :: StatementCacheKey -> IO (Maybe Statement)
-- ^ Retrieve a statement from the cache, or return nothing if it is not found.
--
-- @since 2.14.0
, statementCacheInsert :: StatementCacheKey -> Statement -> IO ()
-- ^ Put a new statement into the cache. An immediate lookup of
-- the statement MUST return the inserted statement for the given
-- cache key. Depending on the implementation, the statement cache MAY
-- choose to evict other statements from the cache within this function.
--
-- @since 2.14.0
, statementCacheClear :: IO ()
-- ^ Remove all statements from the cache. Implementations of this
-- should be sure to call `stmtFinalize` on all statements removed
-- from the cache.
--
-- @since 2.14.0
, statementCacheSize :: IO Int
-- ^ Get the current size of the cache.
--
-- @since 2.14.0
}
data MkStatementCache = MkStatementCache
{ statementCacheLookup :: StatementCacheKey -> IO (Maybe Statement)
-- ^ Retrieve a statement from the cache, or return nothing if it is not found.
--
-- @since 2.14.0
, statementCacheInsert :: StatementCacheKey -> Statement -> IO ()
-- ^ Put a new statement into the cache. An immediate lookup of
-- the statement MUST return the inserted statement for the given
-- cache key. Depending on the implementation, the statement cache MAY
-- choose to evict other statements from the cache within this function.
--
-- @since 2.14.0
, statementCacheClear :: IO ()
-- ^ Remove all statements from the cache. Implementations of this
-- should be sure to call `stmtFinalize` on all statements removed
-- from the cache.
--
-- @since 2.14.0
, statementCacheSize :: IO Int
-- ^ Get the current size of the cache.
--
-- @since 2.14.0
}

codebase uses 4 space indent

Comment on lines +45 to +59

-- | Make a simple statement cache that will cache statements if they are not currently cached.
--
-- @since 2.14.0
mkSimpleStatementCache :: IO MkStatementCache
mkSimpleStatementCache = do
stmtMap <- newIORef Map.empty
pure $ MkStatementCache
{ statementCacheLookup = \sql -> Map.lookup (cacheKey sql) <$> readIORef stmtMap
, statementCacheInsert = \sql stmt ->
modifyIORef' stmtMap (Map.insert (cacheKey sql) stmt)
, statementCacheClear = do
oldStatements <- atomicModifyIORef' stmtMap (\oldStatements -> (Map.empty, oldStatements))
traverse_ stmtFinalize oldStatements
, statementCacheSize = Map.size <$> readIORef stmtMap
}
Co-authored-by: Matt Parsons <parsonsmatt@gmail.com>

@iand675

@iand675

parsonsmatt

@parsonsmatt

Read the original on github.com ↗