如何将Redis简单配置改写为支持长尾词的?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1441个文字,预计阅读时间需要6分钟。
Redis配置文件示例:
propertiesRedis服务器监听的端口port 6379
Redis服务器绑定到的IP地址bind 127.0.0.1
是否开启保护模式,默认为yesprotected-mode yes
Redis数据库的数量,默认为16个数据库databases 16
设置Redis的运行日志级别loglevel notice
设置Redis的数据库文件保存的目录dir /var/lib/redis
设置Redis的数据库文件名dbfilename dump.rdb
设置Redis的AOF持久化文件名appendonly yes
AOF持久化文件保存的目录appendfilename appendonly.aof
AOF持久化是否启用每次写操作后进行同步,默认为everysecappendfsync everysec
设置Redis的客户端连接超时时间timeout 300
设置Redis的最大连接数maxclients 10000
设置Redis的内存最大使用量,单位为字节maxmemory 500000000
设置当达到maxmemory限制时,Redis如何处理maxmemory-policy allkeys-lru
设置Redis的守护进程模式daemonize no
设置Redis的pid文件路径pidfile /var/run/redis/redis.pid
设置Redis的配置文件路径config-file /etc/redis/redis.conf
redis properties# Rides settings #Redis服务器IP redis.host=***.***.***.*** #端口 redis.port=6379 #超时时间 redis.timeout=10000 #可用连接实例的最大数目,默认值为8; #如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。 redis.maxTotal=500 #控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。 redis.maxIdle=20 #等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException; redis.maxWaitMillis=60000 #在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; redis.testOnBorrow=false redisUtil
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.python.modules.synchronize; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class RedisUtil { private static String host;// ip地址 private static String port;// 端口 private static String timeout;// 超时时间 private static String maxIdle;// 最大空闲jedis实例 private static String maxTotal;// 最大jedis连接实例 private static String maxWaitMillis;// 等待可用连接的最大时间 private static String testOnBorrow;// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; private static Properties prop = new Properties(); static { try { String filePath = System.getProperty("user.dir") + "/config/redis.properties"; InputStream in = new BufferedInputStream(new FileInputStream(filePath)); prop.load(in); host = prop.getProperty("redis.host"); port = prop.getProperty("redis.port"); timeout = prop.getProperty("redis.timeout"); maxIdle = prop.getProperty("redis.maxIdle"); maxTotal = prop.getProperty("redis.maxTotal"); maxWaitMillis = prop.getProperty("redis.maxWaitMillis"); testOnBorrow = prop.getProperty("redis.testOnBorrow"); } catch (IOException e) { e.printStackTrace(); } } private static JedisPool jedisPool = null; /** * 6小时 */ public final static int EXRP_HOUR = 60 * 60 * 6; // 6小时 /** * 一天 */ public final static int EXRP_DAY = 60 * 60 * 24; // 一天 /** * 两天 */ public final static int EXRP_twoday = 60 * 60 * 24 * 48; // 两天 /** * 初始化连接池 */ private static void initialPoll(){ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(Integer.parseInt(maxTotal)); config.setMaxIdle(Integer.parseInt(maxIdle)); config.setMaxWaitMillis(Long.parseLong(maxWaitMillis)); config.setTestOnBorrow(Boolean.parseBoolean(testOnBorrow)); jedisPool = new JedisPool(config, host, Integer.parseInt(port), Integer.parseInt(timeout)); } private static synchronized void poolInit(){ if(jedisPool==null){ initialPoll(); } } /** * 同步获取Jedis实例 * @return Jedis */ public synchronized static Jedis getJedis(){ if (jedisPool == null) { poolInit(); } Jedis jedis = null; try { if (jedisPool != null) { jedis = jedisPool.getResource(); } } catch (Exception e) { }finally{ close(jedis); } return jedis; } /** * 释放jedis资源 * @param jedis */ @SuppressWarnings("deprecation") public static void close(final Jedis jedis) { if(jedis!=null && jedisPool!=null){ jedisPool.returnResource(jedis); } } /** * 设置 String * * @param key * @param value */ public synchronized static void setString(String key, String value) { try { getJedis().set(key, value); } catch (Exception e) { } } /** * 设置 过期时间 * * @param key * @param seconds 以秒为单位 * @param value */ public synchronized static void setString(String key, int seconds, String value) { try { getJedis().setex(key, seconds, value); } catch (Exception e) { } } /** * 设置 过期时间 * * @param key * @param seconds 以秒为单位 * @param value */ public synchronized static void setString(byte[] key, int seconds, byte[] value) { try { getJedis().setex(key, seconds, value); } catch (Exception e) { } } /** * 获取String值 * * @param key * @return value */ public synchronized static String getString(String key) { if (getJedis() == null || !getJedis().exists(key)) { return null; } return getJedis().get(key); } /** * 获取String值 * * @param key * @return value */ public synchronized static byte[] getString(byte[] key) { if (getJedis() == null || !getJedis().exists(key)) { return null; } return getJedis().get(key); } /** * 查询key是否存在 * @param key * @return */ public synchronized static Boolean exists(String key) { if (getJedis() == null) { return false; } return getJedis().exists(key); } /** * 查询key是否存在 * @param key * @return */ public synchronized static Boolean exists(byte[] key) { if (getJedis() == null) { return false; } return getJedis().exists(key); } /** * 设置key保存时间 * @param bytekey * @param i */ public synchronized static void expire(byte[] bytekey, int i) { if(getJedis() != null || getJedis().exists(bytekey)){ getJedis().expire(bytekey, i); } } } 序列化存取
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 序列化就是将一个对象转换为二进制的数据流。这样就可以进行传输,或者保存到文件中。如果一个类的对象要想实现序列化,就必须实现serializable接口。在此接口中没有任何的方法,此接口只是作为一个标识,表示本类的对象具备了序列化的能力而已。
* 反序列化:将二进制数据流转换成相应的对象。
* 如果想要完成对象的序列化,则还要依靠ObjectOutputStream和ObjectInputStream,前者属于序列化操作,而后者属于反序列化操作。
*
*/
public class SerializationUtil {
/**
* 序列化对象
*
* @param object
* @return
*/
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
byte[] bytes = null;
try {
// 序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
bytes = baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (oos != null) {
oos.close();
}
if (baos != null) {
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return bytes;
}
/**
* 反序列化对象
*
* @param bytes
* @return
*/
public static Object unserialize(byte[] bytes) {
Object obj = null;
ByteArrayInputStream bais = null;
try {
// 反序列化
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
obj = ois.readObject();
ois.close();
bais.close();
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
/**
* 关闭数据源或目标,调用close()方法可释放对象保存的资源 关闭此流并释放与此流关联的所有系统资源.如果已经关闭该流,则调用此方法无效
*
* @param closeable
* @throws IOException
*/
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 列表序列化(用于Redis整存整取)
*
* @param value
* @return
*/
public static
本文共计1441个文字,预计阅读时间需要6分钟。
Redis配置文件示例:
propertiesRedis服务器监听的端口port 6379
Redis服务器绑定到的IP地址bind 127.0.0.1
是否开启保护模式,默认为yesprotected-mode yes
Redis数据库的数量,默认为16个数据库databases 16
设置Redis的运行日志级别loglevel notice
设置Redis的数据库文件保存的目录dir /var/lib/redis
设置Redis的数据库文件名dbfilename dump.rdb
设置Redis的AOF持久化文件名appendonly yes
AOF持久化文件保存的目录appendfilename appendonly.aof
AOF持久化是否启用每次写操作后进行同步,默认为everysecappendfsync everysec
设置Redis的客户端连接超时时间timeout 300
设置Redis的最大连接数maxclients 10000
设置Redis的内存最大使用量,单位为字节maxmemory 500000000
设置当达到maxmemory限制时,Redis如何处理maxmemory-policy allkeys-lru
设置Redis的守护进程模式daemonize no
设置Redis的pid文件路径pidfile /var/run/redis/redis.pid
设置Redis的配置文件路径config-file /etc/redis/redis.conf
redis properties# Rides settings #Redis服务器IP redis.host=***.***.***.*** #端口 redis.port=6379 #超时时间 redis.timeout=10000 #可用连接实例的最大数目,默认值为8; #如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。 redis.maxTotal=500 #控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。 redis.maxIdle=20 #等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException; redis.maxWaitMillis=60000 #在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; redis.testOnBorrow=false redisUtil
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.python.modules.synchronize; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class RedisUtil { private static String host;// ip地址 private static String port;// 端口 private static String timeout;// 超时时间 private static String maxIdle;// 最大空闲jedis实例 private static String maxTotal;// 最大jedis连接实例 private static String maxWaitMillis;// 等待可用连接的最大时间 private static String testOnBorrow;// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; private static Properties prop = new Properties(); static { try { String filePath = System.getProperty("user.dir") + "/config/redis.properties"; InputStream in = new BufferedInputStream(new FileInputStream(filePath)); prop.load(in); host = prop.getProperty("redis.host"); port = prop.getProperty("redis.port"); timeout = prop.getProperty("redis.timeout"); maxIdle = prop.getProperty("redis.maxIdle"); maxTotal = prop.getProperty("redis.maxTotal"); maxWaitMillis = prop.getProperty("redis.maxWaitMillis"); testOnBorrow = prop.getProperty("redis.testOnBorrow"); } catch (IOException e) { e.printStackTrace(); } } private static JedisPool jedisPool = null; /** * 6小时 */ public final static int EXRP_HOUR = 60 * 60 * 6; // 6小时 /** * 一天 */ public final static int EXRP_DAY = 60 * 60 * 24; // 一天 /** * 两天 */ public final static int EXRP_twoday = 60 * 60 * 24 * 48; // 两天 /** * 初始化连接池 */ private static void initialPoll(){ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(Integer.parseInt(maxTotal)); config.setMaxIdle(Integer.parseInt(maxIdle)); config.setMaxWaitMillis(Long.parseLong(maxWaitMillis)); config.setTestOnBorrow(Boolean.parseBoolean(testOnBorrow)); jedisPool = new JedisPool(config, host, Integer.parseInt(port), Integer.parseInt(timeout)); } private static synchronized void poolInit(){ if(jedisPool==null){ initialPoll(); } } /** * 同步获取Jedis实例 * @return Jedis */ public synchronized static Jedis getJedis(){ if (jedisPool == null) { poolInit(); } Jedis jedis = null; try { if (jedisPool != null) { jedis = jedisPool.getResource(); } } catch (Exception e) { }finally{ close(jedis); } return jedis; } /** * 释放jedis资源 * @param jedis */ @SuppressWarnings("deprecation") public static void close(final Jedis jedis) { if(jedis!=null && jedisPool!=null){ jedisPool.returnResource(jedis); } } /** * 设置 String * * @param key * @param value */ public synchronized static void setString(String key, String value) { try { getJedis().set(key, value); } catch (Exception e) { } } /** * 设置 过期时间 * * @param key * @param seconds 以秒为单位 * @param value */ public synchronized static void setString(String key, int seconds, String value) { try { getJedis().setex(key, seconds, value); } catch (Exception e) { } } /** * 设置 过期时间 * * @param key * @param seconds 以秒为单位 * @param value */ public synchronized static void setString(byte[] key, int seconds, byte[] value) { try { getJedis().setex(key, seconds, value); } catch (Exception e) { } } /** * 获取String值 * * @param key * @return value */ public synchronized static String getString(String key) { if (getJedis() == null || !getJedis().exists(key)) { return null; } return getJedis().get(key); } /** * 获取String值 * * @param key * @return value */ public synchronized static byte[] getString(byte[] key) { if (getJedis() == null || !getJedis().exists(key)) { return null; } return getJedis().get(key); } /** * 查询key是否存在 * @param key * @return */ public synchronized static Boolean exists(String key) { if (getJedis() == null) { return false; } return getJedis().exists(key); } /** * 查询key是否存在 * @param key * @return */ public synchronized static Boolean exists(byte[] key) { if (getJedis() == null) { return false; } return getJedis().exists(key); } /** * 设置key保存时间 * @param bytekey * @param i */ public synchronized static void expire(byte[] bytekey, int i) { if(getJedis() != null || getJedis().exists(bytekey)){ getJedis().expire(bytekey, i); } } } 序列化存取
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 序列化就是将一个对象转换为二进制的数据流。这样就可以进行传输,或者保存到文件中。如果一个类的对象要想实现序列化,就必须实现serializable接口。在此接口中没有任何的方法,此接口只是作为一个标识,表示本类的对象具备了序列化的能力而已。
* 反序列化:将二进制数据流转换成相应的对象。
* 如果想要完成对象的序列化,则还要依靠ObjectOutputStream和ObjectInputStream,前者属于序列化操作,而后者属于反序列化操作。
*
*/
public class SerializationUtil {
/**
* 序列化对象
*
* @param object
* @return
*/
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
byte[] bytes = null;
try {
// 序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
bytes = baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (oos != null) {
oos.close();
}
if (baos != null) {
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return bytes;
}
/**
* 反序列化对象
*
* @param bytes
* @return
*/
public static Object unserialize(byte[] bytes) {
Object obj = null;
ByteArrayInputStream bais = null;
try {
// 反序列化
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
obj = ois.readObject();
ois.close();
bais.close();
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
/**
* 关闭数据源或目标,调用close()方法可释放对象保存的资源 关闭此流并释放与此流关联的所有系统资源.如果已经关闭该流,则调用此方法无效
*
* @param closeable
* @throws IOException
*/
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 列表序列化(用于Redis整存整取)
*
* @param value
* @return
*/
public static

