1Bản Go: store được lắp bằng NewCachingStore(&DBStore{db: db}) và có kiểu ProductStore. Khách đầu tiên mở sản phẩm 42, lời gọi store.FindByID() thực ra chạy vào CachingStore, nên nơi gọi không phải tự viết logic cache.The Go version: store is wired as NewCachingStore(&DBStore{db: db}) and typed as ProductStore. The first shopper opens product 42; store.FindByID() actually runs on CachingStore, so the caller needs no cache logic of its own.
2CachingStore khóa đọc (RLock) rồi tra cache theo mã 42. Map đang trống nên lần này vẫn cần nguồn dữ liệu thật.CachingStore takes a read lock (RLock) and looks up key 42. The map is empty, so this request still needs the real data source.
3Cache miss nên proxy gọi c.next.FindByID(), tức DBStore, với cùng tham số. DBStore chỉ lo truy vấn, không biết có cache.On the miss the proxy calls c.next.FindByID(), i.e. DBStore, with the same arguments. DBStore only runs the query and knows nothing about caching.
4DBStore trả sản phẩm; CachingStore khóa ghi (Lock), lưu vào map rồi trả về. Nếu DBStore lỗi, proxy trả lỗi ngay và không lưu gì vào cache.DBStore returns the product; CachingStore takes the write lock, stores it in the map and returns it. If DBStore fails, the proxy returns the error at once and caches nothing.
5Khách tiếp theo mở cùng sản phẩm qua đúng store đó. Tra mã 42 gặp cache hit nên DBStore không bị chạm tới.The next shopper opens the same product through the same store. The lookup of key 42 is a cache hit, so DBStore is never touched.
6Bản Java không viết proxy tay: Spring bọc bean ProductService trong proxy. Gọi findById(42) đi qua @Cacheable trước: lần đầu trượt cache "products" nên gọi repo.findById(42), các lần sau trả từ cache.The Java version writes no proxy by hand: Spring wraps the ProductService bean in a proxy. findById(42) passes through @Cacheable first: the first call misses the "products" cache and calls repo.findById(42), later calls are served from the cache.
7updatePrice(42, 4590000) đi qua cùng proxy: @Transactional mở transaction, method gọi repo.save(), proxy commit, rồi @CacheEvict xóa khóa 42 để lượt xem sau không nhận giá cũ.updatePrice(42, 4590000) goes through the same proxy: @Transactional opens a transaction, the method calls repo.save(), the proxy commits, then @CacheEvict removes key 42 so later views don't get the old price.
8TimingProxy cho thấy Spring làm gì bên dưới: TimingProxy.wrap(new VnpayPriceApi(), PriceApi.class) gọi Proxy.newProxyInstance để JDK sinh một object implement PriceApi, với TimingProxy làm InvocationHandler.TimingProxy shows what Spring does underneath: TimingProxy.wrap(new VnpayPriceApi(), PriceApi.class) calls Proxy.newProxyInstance so the JDK generates an object implementing PriceApi, with TimingProxy as its InvocationHandler.
9Mọi lời gọi method trên api đều vào invoke(): bấm giờ, method.invoke(target, args) chuyển cho VnpayPriceApi thật, lỗi gốc được ném lại, và finally in thời gian. Giả sử PriceApi có getPrice(), dòng in sẽ như dưới.Every method call on api lands in invoke(): start the clock, method.invoke(target, args) forwards to the real VnpayPriceApi, the original exception is rethrown, and finally prints the time. Assuming PriceApi has getPrice(), the line looks like this.