Mysql + Redis实现排行榜实战(让Redis做好自己的事情)
Posted Java程序员聚集地
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql + Redis实现排行榜实战(让Redis做好自己的事情)相关的知识,希望对你有一定的参考价值。
1)pom.xml
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
2)druid.properties连接池配置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///db_mysql_redis_rank
username=root
password=jianan
initialSize=10
maxActive=20
maxWait=3000
3)jedis.properties
host=127.0.0.1
port=6379
maxTotal=50
maxIdle=10
4)数据库内容
5)JDBCUtils.java
package Demo03.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
private static DataSource ds;
static {
try {
Properties pro = new Properties();
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static DataSource getDataSource() {
return ds;
}
public static void close(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
// 归还连接池
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Statement stmt, Connection conn) {
JDBCUtils.close(null, stmt, conn);
}
}
6)JedisPoolUtils.java
package Demo03.utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class JedisPoolUtils {
private static JedisPool jedisPool;
static {
InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
Properties pro = new Properties();
try {
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
jedisPool = new JedisPool(config, pro.getProperty("host"), Integer.parseInt(pro.getProperty("port")));
}
public static Jedis getJedis() {
return jedisPool.getResource();
}
}
7)domain
User.java
package Demo03.domain;
public class User {
public int uid;
public String uname;
public int gold;
public String toString() {
return "User{" +
"uid=" + uid +
", uname='" + uname + '\'' +
", gold=" + gold +
'}';
}
}
8)MysqlCenter.java
package Demo03.dao;
import Demo03.domain.User;
import Demo03.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class MysqlCenter {
public static User getUserByUid(int paramUid) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
String sql = "select * from t_user where uid = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, paramUid);
rs = pstmt.executeQuery();
User user = null;
if (rs.next()) {
int uid = rs.getInt("uid");
String uname = rs.getString("uname");
int gold = rs.getInt("gold");
user = new User();
user.uid = uid;
user.uname = uname;
user.gold = gold;
}
return user;
} catch (Exception e) {
return null;
} finally {
JDBCUtils.close(rs, pstmt, conn);
}
}
}
9)RedisCenter.java
package Demo03.dao;
import Demo03.utils.JedisPoolUtils;
import redis.clients.jedis.Jedis;
import java.util.Set;
public class RedisCenter {
public static void addToRank(String uname, int gold) {
Jedis jedis = JedisPoolUtils.getJedis();
jedis.zadd("rank", gold, uname);
jedis.close();
}
public static Set<String> getRank() {
Jedis jedis = JedisPoolUtils.getJedis();
Set<String> rankSet = jedis.zrevrange("rank", 0, -1);
jedis.close();
return rankSet;
}
}
10)Main.java 使用
package Demo03;
import Demo03.dao.MysqlCenter;
import Demo03.dao.RedisCenter;
import Demo03.domain.User;
import java.util.Set;
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
User user = MysqlCenter.getUserByUid(i);
RedisCenter.addToRank(user.uname, user.gold);
}
long s = System.currentTimeMillis();
Set<String> rankSet = null;
for (int i = 0; i < 10000; i++) {
rankSet = RedisCenter.getRank();
}
System.out.println(rankSet);
}
}
运行结果
六月 13, 2020 2:46:32 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
[ ]
以上是关于Mysql + Redis实现排行榜实战(让Redis做好自己的事情)的主要内容,如果未能解决你的问题,请参考以下文章