-
Notifications
You must be signed in to change notification settings - Fork 0
/
Redisdemo2Application.java
195 lines (153 loc) · 7.53 KB
/
Redisdemo2Application.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.example.redisdemo2;
import com.example.redisdemo2.applications.leaderboard.LeaderboardService;
import com.example.redisdemo2.service.*;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisKeyValueAdapter;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import java.util.List;
import java.util.Map;
import java.util.Set;
@SpringBootApplication
@Slf4j
public class Redisdemo2Application {
@Autowired
private ValueOperationService valueOperationService;
@Autowired
private HashOperationService hashOperationService;
@Autowired
private ListOperationService listOperationService;
@Autowired
private SetOperationService setOperationService;
@Autowired
private ZSetOperationService zSetOperationService;
@Autowired
private LeaderboardService leaderboardService;
public static void main(String[] args) {
SpringApplication.run(Redisdemo2Application.class, args);
}
@PostConstruct
public void start() {
// === Value operations ===
valueOperationService.addOrUpdate("bob", "1");
valueOperationService.addOrUpdate("alice", "2");
Object val = valueOperationService.get("bob");
log.info(val.toString()); // 1
val = valueOperationService.get("alice");
log.info(val.toString()); // 2
valueOperationService.addWithTtl("rohan", "3", 10); // this entry will expire after 10 secs
// === Hash Operations ===
String hashKey = "userLogin";
hashOperationService.setHashField(hashKey, "rohan", "19-dec-2022");
hashOperationService.setHashField(hashKey, "joe", "21-dec-2022");
hashOperationService.setHashField(hashKey, "sunil", "12-dec-2022");
hashOperationService.setHashField(hashKey, "chhetri", "16-dec-2022");
hashOperationService.setHashField(hashKey, "katie", "23-dec-2022");
hashOperationService.setHashField(hashKey, "jacob", "26-dec-2022");
Map<Object, Object> userLoginActivityCache = hashOperationService.getAllHashFields(hashKey);
log.info(userLoginActivityCache.toString()); // {rohan=19-dec-2022, joe=21-dec-2022, sunil=12-dec-2022, chhetri=16-dec-2022, katie=23-dec-2022, jacob=26-dec-2022}
Object joeLastLogin = hashOperationService.getHashField(hashKey, "joe");
log.info(joeLastLogin.toString()); // 21-dec-2022
hashOperationService.deleteHashField(hashKey, "katie", "jacob"); // deletes entries for "katie" and "jacob" from "userLogin"
userLoginActivityCache = hashOperationService.getAllHashFields(hashKey);
log.info(userLoginActivityCache.toString()); // {rohan=19-dec-2022, joe=21-dec-2022, sunil=12-dec-2022, chhetri=16-dec-2022}
// === List Operations ===
String listKey = "productIds";
listOperationService.addToList(listKey, "0");
listOperationService.addToList(listKey, "1");
listOperationService.addToList(listKey, "1");
listOperationService.addToList(listKey, "2");
listOperationService.addToList(listKey, "2");
listOperationService.addToList(listKey, "3");
listOperationService.addToList(listKey, "4");
List<Object> allProdIdsFromList = listOperationService.getAllFromList(listKey);
log.info(allProdIdsFromList.toString()); // [4, 3, 2, 2, 1, 1, 0]
List<Object> listRange = listOperationService.getListRange(listKey, 0, 2);
log.info(listRange.toString()); // [4, 3, 2]
listRange = listOperationService.getListRange(listKey, 0, 4);
log.info(listRange.toString()); // [4, 3, 2, 2, 1]
listRange = listOperationService.getListRange(listKey, 2, 5);
log.info(listRange.toString()); // [2, 2, 1, 1]
listOperationService.deleteFromList(listKey, 2, "1"); // remove 2 occurrences of 1 from the list
allProdIdsFromList = listOperationService.getAllFromList(listKey);
log.info(allProdIdsFromList.toString()); // [4, 3, 2, 2, 0]
Object lastElementOfList = listOperationService.getFromList(listKey);
log.info(lastElementOfList.toString()); // 0
lastElementOfList = listOperationService.getFromList(listKey);
log.info(lastElementOfList.toString()); // 2
allProdIdsFromList = listOperationService.getAllFromList(listKey);
log.info(allProdIdsFromList.toString()); // [4, 3, 2]
// === Set Operations ===
String setKey = "card";
setOperationService.addToSet(setKey, "iphone", "lamp", "guitar");
Set<Object> cardItems = setOperationService.getSetMembers(setKey);
log.info(cardItems.toString()); // [iphone, guitar, lamp]
// add some more items including some duplicate items
setOperationService.addToSet(setKey, "iphone", "diary", "speaker");
cardItems = setOperationService.getSetMembers(setKey); // [iphone, diary, guitar, lamp, speaker]
log.info(cardItems.toString());
setOperationService.removeFromSet(setKey, "diary", "speaker");
cardItems = setOperationService.getSetMembers(setKey);
log.info(cardItems.toString()); // [lamp, guitar, iphone]
// === Sorted Set Operations ===
String zSetKey = "pageView";
zSetOperationService.addToZSet(zSetKey, "home", 2);
zSetOperationService.addToZSet(zSetKey, "search", 100);
zSetOperationService.addToZSet(zSetKey, "contact", 5);
zSetOperationService.addToZSet(zSetKey, "career", 10);
Set<Object> pagesSortedByViewCount = zSetOperationService.getAllKeys(zSetKey);
log.info(pagesSortedByViewCount.toString());
Set<ZSetOperations.TypedTuple<Object>> pageToViewCount = zSetOperationService.getAllKeyValues(zSetKey);
log.info(pageToViewCount.toString()); // [DefaultTypedTuple [score=2.0, value=home], DefaultTypedTuple [score=5.0, value=contact], DefaultTypedTuple [score=10.0, value=career], DefaultTypedTuple [score=100.0, value=search]]
for (ZSetOperations.TypedTuple<Object> tuple : pageToViewCount) {
System.out.println(tuple.getValue() + ": " + tuple.getScore());
}
// home: 2.0
// contact: 5.0
// career: 10.0
// search: 100.0
zSetOperationService.removeFromZSet(zSetKey, "home", "contact");
pageToViewCount = zSetOperationService.getAllKeyValues(zSetKey);
for (ZSetOperations.TypedTuple<Object> tuple : pageToViewCount) {
System.out.println(tuple.getValue() + ": " + tuple.getScore());
}
// career: 10.0
// search: 100.0
// === Leaderboard example using sorted set===
String leaderboardName = "leagues:2022";
leaderboardService.addScore(leaderboardName, "chirkut", 5000);
leaderboardService.addScore(leaderboardName, "carry", 1000);
leaderboardService.addScore(leaderboardName, "jack", 100);
leaderboardService.addScore(leaderboardName, "dorthy", 300);
leaderboardService.addScore(leaderboardName, "minty", 200);
leaderboardService.addScore(leaderboardName, "monu", 500);
leaderboardService.addScore(leaderboardName, "kaju", 4000);
leaderboardService.addScore(leaderboardName, "emli", 2000);
leaderboardService.addScore(leaderboardName, "katie", 1500);
leaderboardService.addScore(leaderboardName, "grandi", 2200);
Set<ZSetOperations.TypedTuple<Object>> playerAndScores = leaderboardService.getAllPlayerAndScores(leaderboardName);
for (ZSetOperations.TypedTuple<Object> tuple : playerAndScores) {
System.out.println(tuple.getValue() + ": " + tuple.getScore());
}
/*
jack: 100.0
minty: 200.0
dorthy: 300.0
monu: 500.0
carry: 1000.0
katie: 1500.0
emli: 2000.0
grandi: 2200.0
kaju: 4000.0
chirkut: 5000.0
*/
Long rank = leaderboardService.getRank(leaderboardName, "jack");
log.info(rank.toString()); // 9
Double score = leaderboardService.getPlayerScore(leaderboardName, "grandi");
log.info(score.toString()); // 2200.0
}
}