内容摘要:复制packageutil; importredis.clients.jedis.Jedis; /** *Createdbypfliu

复制package util; import redis.clients.jedis.Jedis; /** * Created by pfliu on 2019/06/23. */ public class ShortURLUtil { private static final String SHORT_URL_KEY = "SHORT_URL_KEY"; private static final String LOCALHOST = "http://localhost:4444/"; private static final String SHORT_LONG_PREFIX = "short_long_prefix_"; private static final String CACHE_KEY_PREFIX = "cache_key_prefix_"; private static final int CACHE_SECONDS = 1 * 60 * 60; private final String redisConfig; private final Jedis jedis; public ShortURLUtil(String redisConfig) { this.redisConfig = redisConfig; this.jedis = new Jedis(this.redisConfig); } public String getShortURL(String longURL,短U的设
Decimal decimal) { // 查询缓存 String cache = jedis.get(CACHE_KEY_PREFIX + longURL); if (cache != null) { return LOCALHOST + toOtherBaseString(Long.valueOf(cache), decimal.x); } // 自增 long num = jedis.incr(SHORT_URL_KEY); // 在数据库中保存短-长URL的
免费信息发布网映射关系,可以保存在MySQL中 jedis.set(SHORT_LONG_PREFIX + num, longURL); // 写入缓存 jedis.setex(CACHE_KEY_PREFIX + longURL, CACHE_SECONDS, String.valueOf(num)); return LOCALHOST + toOtherBaseString(num, decimal.x); } /** * 在进制表示中的
云服务器提供商字符集合 */ final static char[] digits = {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, 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}; /** * 由10进制的
服务器租用数字转换到其他进制 */ private String toOtherBaseString(long n, int base) { long num = 0; if (n <0) { num = ((long) 2 * 0x7fffffff) + n + 2; } else { nnum = n; } char[] buf = new char[32]; int charPos = 32; while ((num / base) > 0) { buf[--charPos] = digits[(int) (num % base)]; num /= base; } buf[--charPos] = digits[(int) (num % base)]; return new String(buf, charPos, (32 - charPos)); } enum Decimal { D32(32), D64(64); int x; Decimal(int x) { this.x = x; } } public static void main(String[] args) { for (int i = 0; i <100; i++) { System.out.println(new ShortURLUtil("localhost").getShortURL("www.baidudu.com", Decimal.D32)); System.out.println(new ShortURLUtil("localhost").getShortURL("www.baidu.com", Decimal.D64)); } } } 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.