Created
May 22, 2014 11:13
-
-
Save tkawachi/2b68ca16d1b317c1d1dd to your computer and use it in GitHub Desktop.
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
import com.google.common.cache.{Cache, CacheBuilder} | |
import java.util.concurrent.TimeUnit | |
import net.sf.uadetector.service.UADetectorServiceFactory | |
import net.sf.uadetector.{ReadableUserAgent, UserAgentStringParser} | |
object CachedUserAgentStringParser extends UserAgentStringParser { | |
private val parser = UADetectorServiceFactory.getCachingAndUpdatingParser | |
private val cache: Cache[String, ReadableUserAgent] = | |
CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(2, TimeUnit.HOURS) | |
.build() | |
override def getDataVersion: String = parser.getDataVersion | |
override def parse(userAgentString: String): ReadableUserAgent = { | |
var result = cache.getIfPresent(userAgentString) | |
if (result == null) { | |
result = parser.parse(userAgentString) | |
cache.put(userAgentString, result) | |
} | |
result | |
} | |
override def shutdown() { | |
parser.shutdown() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment