-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3397763
commit 0466df5
Showing
14 changed files
with
209 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ru.ekuzmichev | ||
package store | ||
import common.ProductId | ||
import store.ProductStore.{Product, ProductCandidate, SourceId, SourceState} | ||
|
||
import zio.Task | ||
|
||
class CacheProductStore(decoratee: ProductStore, cacheStateRepository: CacheStateRepository) extends ProductStore: | ||
override def preInitialize(sourceStatesBySourceId: Map[SourceId, SourceState]): Task[Unit] = | ||
decoratee.preInitialize(sourceStatesBySourceId) | ||
|
||
override def checkInitialized(sourceId: SourceId): Task[Boolean] = | ||
decoratee.checkInitialized(sourceId) | ||
|
||
override def checkHasProductId(sourceId: SourceId, productId: ProductId): Task[Boolean] = | ||
decoratee.checkHasProductId(sourceId, productId) | ||
|
||
override def emptyState(sourceId: SourceId): Task[Unit] = | ||
decoratee.emptyState(sourceId) <* replaceStateInCache() | ||
|
||
private def replaceStateInCache(): Task[Unit] = | ||
readAsCacheState().tap(cacheStateRepository.replace).unit | ||
|
||
private def readAsCacheState(): Task[CacheState] = | ||
readAll().map(toCacheState) | ||
|
||
private def toCacheState(sourceStatesBySourceId: Map[SourceId, SourceState]): CacheState = | ||
CacheState( | ||
entries = sourceStatesBySourceId.map { case (sourceId, sourceState) => | ||
CacheStateEntry( | ||
userName = sourceId.userName, | ||
chatId = sourceId.chatId, | ||
products = sourceState.products | ||
) | ||
}.toSeq | ||
) | ||
|
||
override def clearState(sourceId: SourceId): Task[Unit] = | ||
decoratee.clearState(sourceId) <* replaceStateInCache() | ||
|
||
override def readSourceState(sourceId: SourceId): Task[Option[SourceState]] = | ||
decoratee.readSourceState(sourceId) | ||
|
||
override def readAll(): Task[Map[SourceId, SourceState]] = | ||
decoratee.readAll() | ||
|
||
override def updateProductCandidate( | ||
sourceId: SourceId, | ||
productCandidate: ProductCandidate | ||
): Task[Boolean] = | ||
decoratee.updateProductCandidate(sourceId, productCandidate) | ||
|
||
override def resetProductCandidate(sourceId: SourceId): Task[Boolean] = | ||
decoratee.resetProductCandidate(sourceId) | ||
|
||
override def addProduct(sourceId: SourceId, product: Product): Task[Boolean] = | ||
decoratee.addProduct(sourceId, product) <* replaceStateInCache() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ru.ekuzmichev | ||
package store | ||
|
||
import zio.Task | ||
|
||
trait CacheStateRepository: | ||
def read(): Task[CacheState] | ||
def replace(cacheState: CacheState): Task[Unit] |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ru.ekuzmichev | ||
package store | ||
|
||
import config.AppConfig | ||
|
||
import better.files.File as ScalaFile | ||
import zio.{RLayer, ZIO, ZLayer} | ||
|
||
object CacheStateRepositoryLayers: | ||
val file: RLayer[AppConfig, FileCacheStateRepository] = ZLayer.fromZIO { | ||
for { | ||
appConfig <- ZIO.service[AppConfig] | ||
cacheStateFilePath = appConfig.cacheStateFilePath | ||
file <- ZIO.attempt(ScalaFile(cacheStateFilePath)) | ||
_ <- ZIO.log(s"Recreating file $file if not exists") | ||
_ <- ZIO.attempt(file.createIfNotExists(createParents = true)) | ||
} yield new FileCacheStateRepository(cacheStateFilePath) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ru.ekuzmichev | ||
package store | ||
import util.lang.Throwables.failure | ||
|
||
import better.files.{File as ScalaFile, *} | ||
import io.circe.parser.decode | ||
import io.circe.syntax.* | ||
import zio.{Task, ZIO} | ||
|
||
class FileCacheStateRepository(filePath: String) extends CacheStateRepository: | ||
override def read(): Task[CacheState] = | ||
ZIO | ||
.attempt(asScalaFile) | ||
.flatMap(file => | ||
if (file.exists) | ||
ZIO | ||
.attempt(file.contentAsString) | ||
.flatMap(json => | ||
ZIO | ||
.fromEither(decode[CacheState](json)) | ||
.mapError(error => failure(s"Failed to read cache state: $error")) | ||
) | ||
else ZIO.succeed(CacheState.empty) | ||
) | ||
|
||
override def replace(cacheState: CacheState): Task[Unit] = | ||
ZIO.attempt(asScalaFile.overwrite(cacheState.asJson.spaces2)) | ||
|
||
private def asScalaFile: ScalaFile = ScalaFile(filePath) |
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