Skip to content

WIP: GH-20: Create server cache #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions server/src/main/scala/lsug/Meetup.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package lsug

import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
import scala.collection.mutable
import cats.implicits._
import org.http4s.client.Client
import org.http4s.client.dsl.Http4sClientDsl
Expand All @@ -11,32 +14,47 @@ import cats.effect._

import lsug.protocol.Meetup.{MeetupDotCom => P}
import lsug.protocol.Link
import scala.util.matching.Regex
import cats.Monad

object Meetup {

val eventMap: mutable.Map[String, (Int, LocalDateTime)] =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! Thanks for opening this PR; Meetup has been down frequently so it would be nice to have some cache for it 😄.

There are a couple of points to consider:

  1. It would probably be best if we extracted the logic into a separate class and passed it into the apply method. This would mean that it's easier to test the cache behavior as an independent unit
  2. In general we avoid mutable state unless there's a good reason to have it (i.e. bottleneck for performance). We can use a Ref with an immutable map to get a primitive concurrent cache.
  3. In general, Map doesn't provide any guarantees about it's behaviour under concurrent conditions; looking at the implementation of Scala's Map, I think it would be fine, but it's not great to rely on implementation details.

I think sketching out how the solution might be used first before implementing it is a good idea. Here's a starting point.

trait Cache[F[_], K, V] {
  def fetch(key: K, retrieve: K => F[V]): F[V]
}

object Cache {
  def apply[F[_]: Sync, K, V](evictionTime: Duration): Cache[F, K, V] = { 
    ???
  }
}

Thanks for joining the gitter chat and let me know if you encounter any issues.

mutable.Map.empty
def apply[F[_]: Sync](group: P.Group.Id, client: Client[F]): Meetup[F] = {
val dsl = new Http4sClientDsl[F] {}
import dsl._
new Meetup[F] {
val baseUri = uri"https://www.meetup.com"
def event(id: P.Event.Id) = {
val uri = baseUri
//println(s"Event Id show: ${id.show}")
val now = LocalDateTime.now()
val uri: Uri = baseUri
.withPath(s"/${group.show}/events/${id.show}")
client
.expect[String](
GET(
uri,
Accept(MediaRange.`*/*`)
)
)
.map(page =>
"(?<=Attendees \\()[0-9]+(?=\\))".r
.findFirstIn(page)
.map(_.toInt)
.map(
P.Event(new Link(uri.renderString), _)
eventMap.get(id.show) match {
case Some(e) if (ChronoUnit.SECONDS.between(e._2, now) <= 60) =>
F(Option((P.Event(new Link(uri.renderString), e._1))))
case _ => {
val c: F[String] = client
.expect[String](
GET(
uri,
Accept(MediaRange.`*/*`)
)
)
)
val r: F[Option[lsug.protocol.Meetup.MeetupDotCom.Event]] = c.map {
page =>
val reg: Regex = "(?<=Attendees \\()[0-9]+(?=\\))".r
reg
.findFirstIn(page)
.map(_.toInt)
.map { at =>
eventMap.update(id.show, (at, now))
P.Event(new Link(uri.renderString), at)
}
}
r
}
}
}
}
}
Expand Down