Skip to content

Commit ed0bed2

Browse files
committed
add set_short_cached_reference command
1 parent 1c274c5 commit ed0bed2

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Ok Response
8484

8585
### `send_cv`
8686

87-
Send a scraped CV to RT-CV
87+
Send a scraped CV to RT-CV and triggers the `set_cached_reference` for the reference number of this cv if there where matches on this CV
8888

8989
Example input
9090

@@ -150,7 +150,7 @@ Ok Response
150150

151151
### `set_cached_reference`
152152

153-
Save a reference number to the cache
153+
Save a reference number to the cache with a timeout of 3 days
154154
This cache is used to avoid sending the same CV twice or scraping data that has already been scraped
155155

156156
*Note that "send_cv" also executes this function automatically*
@@ -167,6 +167,10 @@ Ok Response
167167
{"type":"ok"}
168168
```
169169

170+
### `set_short_cached_reference`
171+
172+
Same as previouse one except this one has a time out of 12 hours
173+
170174
### `has_cached_reference`
171175

172176
Is there a cache entry for a specific reference number?

api.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,21 +228,23 @@ func (a *API) GetUserSecret(key, encryptionKey string) (UserSecret, error) {
228228
return result, err
229229
}
230230

231+
// SetCacheEntry sets a cache entry for the reference number that expires after the duration
232+
func (a *API) SetCacheEntry(referenceNr string, duration time.Duration) {
233+
a.Cache[referenceNr] = time.Now().Add(duration)
234+
}
235+
231236
// CacheEntryExists returns true if the cache entry exists and is not expired
232237
func (a *API) CacheEntryExists(referenceNr string) bool {
233238
cacheEntryInsertionTime, cacheEntryExists := a.Cache[referenceNr]
234-
if cacheEntryExists {
235-
expired := time.Now().After(
236-
cacheEntryInsertionTime.Add(time.Hour * 72), // 3 days
237-
)
238-
if expired {
239-
delete(a.Cache, referenceNr)
240-
} else {
241-
return true
242-
}
239+
if !cacheEntryExists {
240+
return false
243241
}
244242

245-
return false
243+
expired := time.Now().After(cacheEntryInsertionTime)
244+
if expired {
245+
delete(a.Cache, referenceNr)
246+
}
247+
return !expired
246248
}
247249

248250
// UserSecret represents the json layout of an user secret

main.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,8 @@ func LoopAction(api *API, inputJSON string) (msgType MessageType, msgContent int
224224

225225
DebugToLogfile(referenceNrs)
226226

227-
now := time.Now()
228227
for _, nr := range referenceNrs {
229-
api.Cache[nr] = now
228+
api.SetCacheEntry(nr, time.Hour*72) // 3 days
230229
}
231230

232231
return MessageTypeOk, nil
@@ -255,7 +254,7 @@ func LoopAction(api *API, inputJSON string) (msgType MessageType, msgContent int
255254

256255
hasMatch := false
257256
if api.MockMode {
258-
api.Cache[referenceNr] = time.Now()
257+
api.SetCacheEntry(referenceNr, time.Hour*72)
259258
hasMatch = true
260259
} else {
261260
scanCVBody := json.RawMessage(`{"cv":` + string(input.Content) + `}`)
@@ -274,7 +273,7 @@ func LoopAction(api *API, inputJSON string) (msgType MessageType, msgContent int
274273
hasMatch = response.HasMatches
275274
if hasMatch {
276275
// Only cache the CVs that where matched to something
277-
api.Cache[referenceNr] = time.Now()
276+
api.SetCacheEntry(referenceNr, time.Hour*72) // 3 days
278277
}
279278
}
280279
}
@@ -317,7 +316,7 @@ func LoopAction(api *API, inputJSON string) (msgType MessageType, msgContent int
317316
default:
318317
return returnErr(errors.New("unknown secret"))
319318
}
320-
case "set_cached_reference", "has_cached_reference":
319+
case "set_cached_reference", "set_short_cached_reference", "has_cached_reference":
321320
referenceNr := ""
322321
err = json.Unmarshal(input.Content, &referenceNr)
323322
if err != nil {
@@ -328,11 +327,16 @@ func LoopAction(api *API, inputJSON string) (msgType MessageType, msgContent int
328327
return returnErr(errors.New("reference number cannot be an empty string"))
329328
}
330329

331-
if input.Type == "set_cached_reference" {
332-
api.Cache[referenceNr] = time.Now()
330+
switch input.Type {
331+
case "set_cached_reference":
332+
api.SetCacheEntry(referenceNr, time.Hour*72) // 3 days
333+
return MessageTypeOk, nil
334+
case "set_short_cached_reference":
335+
api.SetCacheEntry(referenceNr, time.Hour*12) // 0.5 days
333336
return MessageTypeOk, nil
334337
}
335338

339+
// has_cached_reference
336340
hasCachedReference := api.CacheEntryExists(referenceNr)
337341
DebugToLogfile("has_cached_reference", referenceNr, ">", hasCachedReference)
338342

0 commit comments

Comments
 (0)