java 秒杀缓存 详细代码
时间: 2024-05-12 07:13:24 浏览: 88
java高并发买秒杀完整代码
5星 · 资源好评率100%
Java 秒杀系统中使用缓存可以提高系统的并发处理能力和性能,减轻数据库的压力。下面是一个简单的基于 Redis 的秒杀缓存实现代码示例:
1. 引入 Redis 相关依赖:
```xml
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
```
2. 创建 Redis 连接池:
```java
public class RedisPoolUtil {
private static JedisPool jedisPool = null;
static {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(10);
jedisPoolConfig.setMaxTotal(100);
jedisPoolConfig.setMaxWaitMillis(10000);
jedisPool = new JedisPool(jedisPoolConfig, "localhost", 6379);
}
public static Jedis getJedis() {
return jedisPool.getResource();
}
public static void releaseJedis(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
}
```
3. 编写秒杀缓存相关代码:
```java
public class SeckillRedisDaoImpl implements SeckillRedisDao {
private static final String SECKILL_KEY_PREFIX = "seckill:";
private static final String SECKILL_STOCK_KEY_PREFIX = "seckill:stock:";
private static final String SECKILL_USER_KEY_PREFIX = "seckill:user:";
@Override
public boolean putSeckill(long seckillId, long startTime, long endTime) {
Jedis jedis = RedisPoolUtil.getJedis();
try {
String key = SECKILL_KEY_PREFIX + seckillId;
Map<String, String> map = new HashMap<>();
map.put("startTime", String.valueOf(startTime));
map.put("endTime", String.valueOf(endTime));
jedis.hmset(key, map);
return true;
} finally {
RedisPoolUtil.releaseJedis(jedis);
}
}
@Override
public Seckill getSeckill(long seckillId) {
Jedis jedis = RedisPoolUtil.getJedis();
try {
String key = SECKILL_KEY_PREFIX + seckillId;
Map<String, String> map = jedis.hgetAll(key);
if (map == null || map.isEmpty()) {
return null;
}
Long startTime = Long.valueOf(map.get("startTime"));
Long endTime = Long.valueOf(map.get("endTime"));
return new Seckill(seckillId, startTime, endTime);
} finally {
RedisPoolUtil.releaseJedis(jedis);
}
}
@Override
public boolean putStock(long seckillId, long stockCount) {
Jedis jedis = RedisPoolUtil.getJedis();
try {
String key = SECKILL_STOCK_KEY_PREFIX + seckillId;
jedis.set(key, String.valueOf(stockCount));
return true;
} finally {
RedisPoolUtil.releaseJedis(jedis);
}
}
@Override
public long getStock(long seckillId) {
Jedis jedis = RedisPoolUtil.getJedis();
try {
String key = SECKILL_STOCK_KEY_PREFIX + seckillId;
String value = jedis.get(key);
if (value == null) {
return -1;
}
return Long.valueOf(value);
} finally {
RedisPoolUtil.releaseJedis(jedis);
}
}
@Override
public boolean putUser(long seckillId, long userId) {
Jedis jedis = RedisPoolUtil.getJedis();
try {
String key = SECKILL_USER_KEY_PREFIX + seckillId;
jedis.sadd(key, String.valueOf(userId));
return true;
} finally {
RedisPoolUtil.releaseJedis(jedis);
}
}
@Override
public boolean isUserExist(long seckillId, long userId) {
Jedis jedis = RedisPoolUtil.getJedis();
try {
String key = SECKILL_USER_KEY_PREFIX + seckillId;
return jedis.sismember(key, String.valueOf(userId));
} finally {
RedisPoolUtil.releaseJedis(jedis);
}
}
}
```
以上代码实现了 Redis 缓存中秒杀商品基本信息、库存数量和用户购买记录的缓存操作。需要注意的是,对于秒杀缓存的实现,还需要考虑缓存的更新和缓存与数据库之间的一致性等问题。
阅读全文