Posts

Showing posts from 2021

zio and refilling a cache

zio and refilling a cache In some small web app server projects I sometimes need to cache some data to reduce latency and the impact of queries on a “weak” backend database. We can use alot of clever caching tools but sometimes I just need something quick and dirty. If you are using ZIO-style services, you may want the cache to be independent of other services and maintain the cache internally to the service environment. There are many ways to cache remote data, for example, you could also use ZQuery or the newer ZCache. Let’s assume that we want to do something simple: case class PeopleCache ( byId : Map [ UUID , Person ] = Map ( ) , last : LocalDateTime = LocalDateTime . now ( ) ) object PeopleCache : def createFromList ( people : List [ Person ] ) = ? ? ? trait Service : def search ( qstring : String ) : IO [ Exception , Option [ Person ] val live = ? ? ? class PeopleServiceImpl ( cache : RefM [ PeopleCache ] , db...

scala3 + zio + dotty-cps-async field report

scala3 + zio + dotty-cps-async I’ve put multiple, small but “smart”, LOB applications into production based on scala and zio. Recently, I also did that with a scala3+zio project. Because of the amount of twisty, async logic, scala3 and zio made it easy for the key blocking and tackling areas. What I like about scala3: Fewer braces. This seems to make a huge difference in code comprehension. Reading code I wrote just yesterday is easier than with the less-braces model. I don’t know why but it just works. Macros, types, extensions, improved type inference: I do not use alot of arcane scala features in my code (libraries have at it!). I have noticed I write less types, macros from other packages seem more useful and I seem to write alot less boilerplate cade. What I like about zio: I still like all the things I liked about zio, large list. Most importantly, it takes care of most of my needs for solving my problems without going outside the system and having...

zio quick read: Extracting the executor for a service

zio quick read: Extracting the executor for a service Let’s say you have the need to obtain an Executor from the default zio runtime and use it to create a service. Let’s assume you have a simple function to make your service. This function has no zio knowledge. def mkService ( executor : java . util . concurrent . Executor ) : MyService = ? ? ? Let’s say that you want to use the zio default runtime executor. For example, your service needs to use a specific Executor to power HttpClient –the new async http client in java 11. Otherwise, HttpClient uses a common, shared thread pool which may or may not be what you want. The executor is in the ZIO environment and we want to create a layer so that MyService is in the environment R , the environment, for all effects in the program. We need to pull the executor out the environment and add the derived service as a layer. Here’s the code (this is the hard way): type MyEnv = Has [ MyService ] val resources...