1
package com.tasks.cache.web
3
import com.tasks.cache.Cache
4
import org.springframework.core.serializer.DefaultSerializer
5
import org.springframework.web.bind.annotation.*
6
import org.springframework.web.bind.annotation.RequestMethod.*
7
import java.io.ByteArrayOutputStream
8
import java.io.Serializable
11
class CacheController(private val cache: Cache<Serializable>) {
12
@RequestMapping(value = "/cache/{key}", method = [GET])
13
fun get(@PathVariable("key") key: String): ByteArray? {
14
val result = cache.get(key)
15
return serialize(result)
18
private fun serialize(result: Serializable?): ByteArray? {
19
return if (result != null) {
20
if (result is ByteArray)
23
val outputStream = ByteArrayOutputStream()
24
DefaultSerializer().serialize(result, outputStream)
25
outputStream.toByteArray()
32
@RequestMapping(value = "/cache/{key}", method = [PUT, POST])
33
fun put(@PathVariable("key") key: String, @RequestParam expiry: Long?, @RequestBody value: ByteArray) {
37
cache.put(key, value, expiry)
41
@RequestMapping(value = "/cache/{key}", method = [DELETE] )
42
fun delete(@PathVariable("key") key: String): ByteArray? {
43
var result = cache.delete(key)
44
return serialize(result)