Can I use a timestamp parameter for cache invalidation?

I am considering a scheme for passing a timestamp value in a cached response, and then sending it back to the service to perform cache invalidation. I'm wondering if that's a valid approach. Is that a common approach?

For example, if I have a service that returns a "Foo" record, which is a combination of DB lookups and calculations. Let's say Foo is identified with a key. To speed things up, I want to keep a local cache of results.

The problem is, across a bank of servers, the local caches will get out of sync, because the calculated results will change over time.

So my thought is:

  • for a request, calculate a Foo
  • store the Foo in local cache with an "as of" timestamp
  • return the Foo along with the "as of" timestamp
  • when the client wants to fetch a Foo, it passes the key as well as the "as of" timestamp.
  • if the server finds the key in the local cache, and the "as of" timestamp in cache is newer than the "as of" parameter, return the cached Foo with the Foo, key and "as of" timestamp
  • otherwise recalculate the Foo, cache it, and return the (new) Foo, key and (new) "as of" timestamp

The client would need to keep track of both the key and the "as of" timestamp, but then it would essentially control when service instances refresh their local cache, without the service instances talking to each other or some back-end storage for coordination.

If the client wanted to force a cache refresh, it could pass "as of" of max value, which would force a refresh on any service instance it hits.

Certainly I could put a caching layer in front of my service, and I could also save results in a distributed cache. Those solutions have their own data replication and expiration issues too. But for the purpose of this question, I really want to consider local caching & consider the architectural questions separately.

Q: Does that work? Is it valid or good to use a timestamp parameter as a mechanism for cache invalidation?

Source: Can I use a timestamp parameter for cache invalidation?
From: StackExchange Network

Java question re System.out.println

Beginner to Java, creating a library system with methods such as borrowBook & returnBook.

public void borrowBooks(int borrowBooks)
    noOfBooks = noOfBooks + borrowBooks;

How do I make it print out to the console the amount of books currently on loan after this method has been used, assuming the value before this was zero.

Also at the end how do I print all of the borrowers details that have been entered. Such as

[firstName] [LastName]
Library Number: [libraryNumber]
Number of loans: [noOfBooks]

Source: Java question re System.out.println
From: StackExchange Network

What was the first programming language with Enumerations?

I'm reading about Swift enum's in the Swift Programming Language guide and the text was comparing the differences between Swift's enum and C's enum. This made me curious as to where enumerations came from originally. I did search online before asking and even asked a few people and they assumed C. (I suppose I'm trying to confirm if enumerations came from C originally.)

What was the first programming language to include enumerations?

Source: What was the first programming language with Enumerations?
From: StackExchange Network

How appropriate is copying/learning from other software similar to yours? [on hold]

When developing a new software, or adding a new feature to your existing software, how ethical/appropriate to copy/learn/get ideas from other software in the same domain? I am not asking about that copying special features, or any copyrighted property, but merely getting inspired from basic design choices and basic domain knowledge.

A little example: You are developing a web app that sells DVD's. But you don't have a solid idea on how customers would like to search for a particular dvd, like what parameters should be used? What filters to be put on search results? What set of pages/what procedure should customer go through to buy the movie?

Is it OK to learn from open source/free trial software on the same subject in such cases?

Source: How appropriate is copying/learning from other software similar to yours? [on hold]
From: StackExchange Network