Hash 的一些案例
677字约2分钟
2024-08-10
社交网站的网址点击追踪机制
has 操作涉及 api
hset:设置key,以及value中的key和value
hincrBy:设置key和value中的key,增加次数统计
hget:设置key和value中的key,获取统计的次数
public class RedisHash {
private static Jedis jedis = new Jedis("127.0.0.1", 6379);
private static final String[] X36_ARRAY = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z".split(",");
public static void main(String[] args) {
// 短网址追踪案例
String shortUrl = shortUrl("http://www.baidu.com?id=123123&en=dfsdfsd");
incrementShortUrlAccessCount(shortUrl);
long shortUrlAccessCount = getShortUrlAccessCount(shortUrl);
System.out.println("短链接地址访问次数:" + shortUrl);
}
/**
* 将长链接地址转为短链接地址
* @param url
* @return
*/
public static String shortUrl(String url) {
StringBuffer buffer = new StringBuffer();
Long shortUrlSeed = jedis.incr("short_url_seed");
while (shortUrlSeed > 0) {
buffer.append(X36_ARRAY[(int) (shortUrlSeed % 36)]);
shortUrlSeed = shortUrlSeed / 36;
}
String shortUrl = buffer.reverse().toString();
jedis.hset("short_url_access_count", shortUrl, "0");
jedis.hset("short_url_mapping", shortUrl, url);
return shortUrl;
}
/**
* 给短链接地址进行访问次数的增长
* @param shortUrl
*/
public static void incrementShortUrlAccessCount(String shortUrl) {
jedis.hincrBy("short_url_access_count", shortUrl, 1);
}
/**
* 获取短链接的访问次数
* @param shortUrl
* @return
*/
public static long getShortUrlAccessCount(String shortUrl) {
return Long.parseLong(jedis.hget("short_url_access_count", shortUrl));
}
}
blog 重构为 hash 存储
key -> value(<key, value>)
hexists:判断这个key中的key是否存在
hmset:设置一个map格式数据
hgetAll:获取这个key的所有数据
hincrBy:让key中key的value增加自定义大小的值
/**
* @ClassName RedisBlogDemo
* @Desciption
* @Author MaRui
* @Date 2021/8/28 10:49
* @Version 1.0
*/
public class RedisBlogDemo {
private static Jedis jedis = new Jedis("127.0.0.1", 6379);
public static void main(String[] args) {
RedisBlogDemo demo = new RedisBlogDemo();
// 发表一篇博客
long id = demo.getBlogId();
Map<String, String> blog = new HashMap<>();
blog.put("id", String.valueOf(id));
blog.put("title", "Redis");
blog.put("content", "学习Redis内容");
blog.put("author", "MaRui");
blog.put("time", "2021-08-28 13:50:22");
demo.publishBlod(id, blog);
// 更新一篇博客
blog.put("title", "更新Redis");
blog.put("content", "更新学习Redis内容");
demo.updateBlog(id, blog);
// 查看博客详细内容
Map<String, String> blogDetail = demo.findBlogById(id);
System.out.println("查看博客的详细内容:" + blogDetail);
// 点赞
demo.incrementBlogLikeCount(id);
// 自己查看博客内容和点赞次数
blogDetail = demo.findBlogById(id);
System.out.println("自己查看博客的详细内容:" + blogDetail);
}
/**
* 生成一个博客 id
* @return
*/
public long getBlogId() {
return jedis.incr("blog_id_counter");
}
/**
* 发布一篇博客
*/
public boolean publishBlod(long id, Map<String, String> blog) {
if (jedis.hexists("article::" + id, "title")) {
return false;
}
String updateContent = blog.get("content");
if (updateContent != null && !"".equals(updateContent)) {
blog.put("content_length", String.valueOf(updateContent.length()));
}
jedis.hmset("article::" + id, blog);
return true;
}
/**
* 查看一篇博客
* @param id
* @return
*/
public Map<String, String> findBlogById(long id) {
Map<String, String> blog = jedis.hgetAll("article::" + id);
incrementBlogviewCount(id);
return blog;
}
/**
* 更新一篇博客
* @param id
*/
public void updateBlog(long id, Map<String, String> updateBlog) {
String updateContent = updateBlog.get("content");
if (updateContent != null && !"".equals(updateContent)) {
updateBlog.put("content_length", String.valueOf(updateContent.length()));
}
jedis.hmset("article::" + id, updateBlog);
}
/**
* 对博客进行点赞
* @param id
*/
public void incrementBlogLikeCount (long id) {
jedis.hincrBy("article::"+ id, "like_count", 1);
}
/**
* 增加博客浏览次数
* @param id
*/
public void incrementBlogviewCount(long id) {
jedis.hincrBy("article:"+ id, "view_count", 1);
}
}