Hi there,
I’m trying to setup a caching system with Workers KV. Testing the concept with the following simple Worker code:
const KV_CACHE_KEY = 'test_cache_key'
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
if (request.method === 'POST') {
let data = await request.text()
DevKV.put(KV_CACHE_KEY, data)
return new Response(data, { status: 200, statusText: 'OK' })
}
else if (request.method === 'GET') {
let data = await DevKV.get(KV_CACHE_KEY)
return new Response(data, { status: 200, statusText: 'OK' })
}
}
And testing it with the following Python script:
import requests
import uuid
import time
ENDPOINT_URL = 'https://site.com/endpoint'
DELAY = 60 # This should be enough for KV update
for i in range(100):
random_str = uuid.uuid4().hex
print('Writing: %s' % random_str)
r = requests.post(ENDPOINT_URL, data=random_str)
if random_str not in r.text:
print('Something went wrong')
break
time.sleep(DELAY)
r2 = requests.get(ENDPOINT_URL + '?nocache=%s' % uuid.uuid4().hex) # adding random query string for each request
if random_str in r2.text:
print('Successfully Updated')
else:
print('Failed to Update. Current Value: ' + r2.text)
So the KV is updating inconsistently. Sometimes it updates, sometimes doesn’t.
Script output:
Writing: 90fa0dcc81de4829bc15e2dd08c43955
Successfully Updated
Writing: e5a330c7cb5943108d1bfa9ad40af6c3
Failed to Update. Current Value: 90fa0dcc81de4829bc15e2dd08c43955
Writing: 7e5fb1a49e444033897c9d1a689d0327
Failed to Update. Current Value: 90fa0dcc81de4829bc15e2dd08c43955
Writing: 75678bd75f8a44b7b1a5ec0e70bc16de
Failed to Update. Current Value: 90fa0dcc81de4829bc15e2dd08c43955
Writing: d9a00fe03c6b4db9b1880dc2087c773e
Successfully Updated
Writing: bd95b2b2b76c4134bf7d7a960b0d98d7
Successfully Updated
Writing: 329d770d57764985953f9141b21e9237
Failed to Update. Current Value: bd95b2b2b76c4134bf7d7a960b0d98d7
Writing: 26c1526d0eb94d878fdab8e01c8ef15e
Failed to Update. Current Value: bd95b2b2b76c4134bf7d7a960b0d98d7
Writing: 936d85ad54654b389ee8223645ff3051
Failed to Update. Current Value: bd95b2b2b76c4134bf7d7a960b0d98d7
Writing: a8fd98f9f97b4f5b8a16792b705f117a
Failed to Update. Current Value: bd95b2b2b76c4134bf7d7a960b0d98d7
Writing: e4773ac9175a4254bb7e3226f446b055
Failed to Update. Current Value: bd95b2b2b76c4134bf7d7a960b0d98d7
Writing: b13ed7dbad0b4a52b3be44bf378afffb