Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2204

Update viewCount on every hit in redis

$
0
0

I'm using Redis to cache an object. I want to maintain a count of hits on redis for a particular key. For example,This is the key value pair I'm storing key value as below in Redis.<customerId, customerDetails>

public class CustomerDetails {public String name;public String phoneNumber;public int viewCount;}

I want to update viewCount whenever redis cache is hit. I'm not sure how can I use opsforValue to increment a value in an object.

I've tried out something like this: String viewCount = valueOps.get(String.valueOf(customerId)); System.out.println("viewCount " + viewCount ); if(viewCount == null) { System.out.println("adding new key value"); valueOps.set(String.valueOf(customerId), maxRate, 25, TimeUnit.SECONDS); allow = true; } else { System.out.println("increasing value"); valueOps.increment(String.valueOf(customerId), 1); if(Integer.parseInt(viewCount ) > 0) { allow = true; } }


Viewing all articles
Browse latest Browse all 2204

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>