client: update time stamp for non-changed index

When an index is stale but there are no changes to it at the server
side, update the retrieved timestamp for the index. Otherwise, the
server is asked for index change on each future invocation, which
cancels the benefits of a cache. Further, index retrieval is called on
each client.Get, so loading the vulnerabilities can be slowed down too.

Change-Id: Ie4814d5f74b412e746247097939c3c09cbbb87f5
Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/351869
Run-TryBot: Zvonimir Pavlinovic <zpavlinovic@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Julie Qiu <julie@golang.org>
Reviewed-by: Roland Shoemaker <roland@golang.org>
Trust: Julie Qiu <julie@golang.org>
diff --git a/client/client.go b/client/client.go
index 74a53af..27b1f4f 100644
--- a/client/client.go
+++ b/client/client.go
@@ -126,6 +126,12 @@
 	}
 	defer resp.Body.Close()
 	if cachedIndexRetrieved != nil && resp.StatusCode == http.StatusNotModified {
+		// If status has not been modified, this is equivalent to returning the
+		// same index. We update the timestamp so the next cache index read does
+		// not require a roundtrip to the server.
+		if err = hs.cache.WriteIndex(hs.dbName, cachedIndex, time.Now()); err != nil {
+			return nil, err
+		}
 		return cachedIndex, nil
 	}
 	if resp.StatusCode != http.StatusOK {