or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

authentication.mdclient-side-caching.mdclustering.mdcommands-operations.mdconnection-management.mdcore-clients.mdexceptions.mdindex.mdmodules.mdparameters.mdpubsub.mdtransactions-pipelining.md

client-side-caching.mddocs/

0

# Client-Side Caching

1

2

Jedis provides comprehensive client-side caching support to improve performance by reducing network round-trips and server load. The caching system supports various eviction policies, TTL, and statistics.

3

4

## Capabilities

5

6

### Cache Interface

7

8

Core caching abstraction for client-side caching implementations.

9

10

```java { .api }

11

/**

12

* Cache interface for storing and retrieving cached values

13

*/

14

public interface Cache {

15

/**

16

* Flush all entries from the cache

17

*/

18

void flush();

19

20

/**

21

* Flush all entries from all caches

22

*/

23

void flushAll();

24

25

/**

26

* Invalidate a specific cache entry

27

* @param key Cache key to invalidate

28

*/

29

void invalidate(CacheKey key);

30

31

/**

32

* Get a cached value

33

* @param key Cache key

34

* @return Cached value or null if not found

35

*/

36

Cacheable getValue(CacheKey key);

37

38

/**

39

* Store a value in the cache

40

* @param key Cache key

41

* @param value Value to cache

42

*/

43

void setValue(CacheKey key, Cacheable value);

44

45

/**

46

* Get cache statistics

47

* @return Cache statistics

48

*/

49

CacheStats getStats();

50

}

51

```

52

53

### Cache Configuration

54

55

Configuration object for customizing cache behavior.

56

57

```java { .api }

58

/**

59

* Configuration for cache instances

60

*/

61

public class CacheConfig {

62

/**

63

* Create cache config with size and TTL

64

* @param maxSize Maximum number of entries

65

* @param ttlSeconds Time-to-live in seconds

66

*/

67

public CacheConfig(int maxSize, int ttlSeconds);

68

69

/**

70

* Set maximum cache size

71

* @param maxSize Maximum number of entries

72

* @return This config for chaining

73

*/

74

public CacheConfig maxSize(int maxSize);

75

76

/**

77

* Set time-to-live for entries

78

* @param ttlSeconds TTL in seconds

79

* @return This config for chaining

80

*/

81

public CacheConfig ttl(int ttlSeconds);

82

83

/**

84

* Set eviction policy

85

* @param evictionPolicy Eviction policy to use

86

* @return This config for chaining

87

*/

88

public CacheConfig evictionPolicy(EvictionPolicy evictionPolicy);

89

90

public int getMaxSize();

91

public int getTtlSeconds();

92

public EvictionPolicy getEvictionPolicy();

93

}

94

```

95

96

### Default Cache Implementation

97

98

Built-in cache implementation with LRU eviction.

99

100

```java { .api }

101

/**

102

* Default cache implementation using LRU eviction

103

*/

104

public class DefaultCache implements Cache {

105

/**

106

* Create cache with maximum size

107

* @param maxSize Maximum number of entries

108

*/

109

public DefaultCache(int maxSize);

110

111

/**

112

* Create cache with configuration

113

* @param config Cache configuration

114

*/

115

public DefaultCache(CacheConfig config);

116

}

117

```

118

119

### Cache Entry

120

121

Wrapper for cached values with metadata.

122

123

```java { .api }

124

/**

125

* Represents a cached entry with metadata

126

*/

127

public class CacheEntry {

128

/**

129

* Get the cached value

130

* @return Cached value

131

*/

132

public Object getValue();

133

134

/**

135

* Get timestamp when entry was cached

136

* @return Timestamp in milliseconds

137

*/

138

public long getTimestamp();

139

140

/**

141

* Check if entry has expired

142

* @return true if expired

143

*/

144

public boolean isExpired();

145

}

146

```

147

148

### Cache Key

149

150

Key representation for cache entries.

151

152

```java { .api }

153

/**

154

* Key for cache entries

155

*/

156

public class CacheKey {

157

public CacheKey(String key);

158

public String getKey();

159

public int hashCode();

160

public boolean equals(Object obj);

161

}

162

```

163

164

### Cache Statistics

165

166

Statistics and metrics for cache performance monitoring.

167

168

```java { .api }

169

/**

170

* Cache performance statistics

171

*/

172

public class CacheStats {

173

/**

174

* Get number of cache hits

175

* @return Hit count

176

*/

177

public long getHits();

178

179

/**

180

* Get number of cache misses

181

* @return Miss count

182

*/

183

public long getMisses();

184

185

/**

186

* Get hit ratio

187

* @return Hit ratio between 0.0 and 1.0

188

*/

189

public double getHitRatio();

190

191

/**

192

* Get current number of entries

193

* @return Entry count

194

*/

195

public long getSize();

196

197

/**

198

* Get number of evictions

199

* @return Eviction count

200

*/

201

public long getEvictions();

202

}

203

```

204

205

### Eviction Policies

206

207

Cache eviction strategies for managing memory usage.

208

209

```java { .api }

210

/**

211

* Base eviction policy interface

212

*/

213

public interface EvictionPolicy {

214

/**

215

* Select entry to evict

216

* @return Cache key to evict

217

*/

218

CacheKey selectEvictionCandidate();

219

220

/**

221

* Notify policy of entry access

222

* @param key Accessed key

223

*/

224

void onAccess(CacheKey key);

225

}

226

227

/**

228

* Least Recently Used eviction policy

229

*/

230

public class LRUEviction implements EvictionPolicy {

231

public LRUEviction();

232

}

233

```

234

235

## Usage Examples

236

237

### Basic Caching Setup

238

239

```java

240

import redis.clients.jedis.csc.*;

241

242

// Create cache with configuration

243

CacheConfig config = new CacheConfig(1000, 300) // 1000 entries, 5min TTL

244

.evictionPolicy(new LRUEviction());

245

246

Cache cache = new DefaultCache(config);

247

248

// Use with JedisPooled for automatic caching

249

JedisPooled jedis = new JedisPooled("localhost", 6379,

250

DefaultJedisClientConfig.builder().build(),

251

config);

252

```

253

254

### Manual Cache Management

255

256

```java

257

// Manual cache operations

258

CacheKey key = new CacheKey("user:123");

259

Cacheable value = new DefaultCacheable("John Doe");

260

261

cache.setValue(key, value);

262

Cacheable cached = cache.getValue(key);

263

264

// Check statistics

265

CacheStats stats = cache.getStats();

266

System.out.println("Hit ratio: " + stats.getHitRatio());

267

System.out.println("Cache size: " + stats.getSize());

268

```

269

270

### Invalidation Patterns

271

272

```java

273

// Invalidate specific key

274

cache.invalidate(new CacheKey("user:123"));

275

276

// Flush all entries

277

cache.flush();

278

279

// Flush across all cache instances

280

cache.flushAll();

281

```

282

283

## Integration with JedisPooled

284

285

Client-side caching integrates seamlessly with JedisPooled for transparent performance improvements:

286

287

```java

288

// JedisPooled with caching

289

CacheConfig cacheConfig = new CacheConfig(2000, 600);

290

JedisPooled jedis = new JedisPooled("localhost", 6379,

291

DefaultJedisClientConfig.builder().build(),

292

cacheConfig);

293

294

// Operations are automatically cached

295

String value = jedis.get("key"); // Cache miss - fetches from Redis

296

String cached = jedis.get("key"); // Cache hit - returns from local cache

297

```