“Redis persistence”与“Vertx: Future异步操作设计”:页面之间的差异

来自WHY42
(页面间差异)
无编辑摘要
 
 
第1行: 第1行:
Redis提供如下的持久化机制:
=Future相关操作的设计=
;RDB (Redis Database): The RDB persistence performs point-in-time snapshots of your dataset at specified intervals.
== AsyncResult ==
;AOF (Append Only File): The AOF persistence logs every write operation received by the server, that will be played again at server startup, reconstructing the original dataset. Commands are logged using the same format as the Redis protocol itself, in an append-only fashion. Redis is able to rewrite the log in the background when it gets too big.
AsyncResult表示一个异步操作的结果,这个结果要么是成功的,要么是失败的。
;No persistence: If you wish, you can disable persistence completely, if you want your data to just exist as long as the server is running.
<syntaxhighlight lang="java">
;RDB + AOF: It is possible to combine both AOF and RDB in the same instance. Notice that, in this case, when Redis restarts the AOF file will be used to reconstruct the original dataset since it is guaranteed to be the most complete.
interface AsyncResult<T> {
=RDB=
    /* 获取异步结果,或者失败原因。倘若失败,那么结果为null */
==RDB advantages==
    T getResult();
* RDB is a very compact single-file point-in-time representation of your Redis data. RDB files are perfect for backups. For instance you may want to archive your RDB files every hour for the latest 24 hours, and to save an RDB snapshot every day for 30 days. This allows you to easily restore different versions of the data set in case of disasters.
    Throwable cause();
* RDB is very good for disaster recovery, being a single compact file that can be transferred to far data centers, or onto Amazon S3 (possibly encrypted).
    boolean succeeded();
* RDB maximizes Redis performances since the only work the Redis parent process needs to do in order to persist is forking a child that will do all the rest. The parent instance will never perform disk I/O or alike.
    boolean failed();
* RDB allows faster restarts with big datasets compared to AOF.
On replicas, RDB supports partial resynchronizations after restarts and failovers.


== RDB disadvantages==
    /* 对结果进行转换。如果成功,那么可以将成功的结果转换为另一种形式;否则可以得到一个替代的结果。*/
* RDB is NOT good if you need to minimize the chance of data loss in case Redis stops working (for example after a power outage). You can configure different save points where an RDB is produced (for instance after at least five minutes and 100 writes against the data set, but you can have multiple save points). However you'll usually create an RDB snapshot every five minutes or more, so in case of Redis stopping working without a correct shutdown for any reason you should be prepared to lose the latest minutes of data.
    <V> AsyncResult<V> map(Function<T, V> mapper);
* RDB needs to fork() often in order to persist on disk using a child process. Fork() can be time consuming if the dataset is big, and may result in Redis to stop serving clients for some millisecond or even for one second if the dataset is very big and the CPU performance not great. AOF also needs to fork() but you can tune how often you want to rewrite your logs without any trade-off on durability.
    <V> AsyncResult<V> map(V value);
    <V> AsyncResult<V> mapEmpty();
    AsyncResult<T> otherwise(T value);
    AsyncResult<T> otherwise(Function<Throwable, T> mapper);
    AsyncResult<T> otherwiseEmpty();
}
</syntaxhighlight>


==Future==
Future表示一个尚未完成的异步操作,并继承自AsyncResult
<syntaxhighlight lang="java">
interface Future<T>
    extends AsyncResult<T> {
    boolean isComplete();
    Future<T> onComplete(Handler<AsyncResult<T>> handler);
    Future<T> onSuccess(Handler<T> handler);
    Future<T> onFailure(Handler<Throwable> handler);
    <V> Future<V> compose(Function<T, Future<V>> mapper);
    <V> Future<V> compose(Function<T, Future<V>> successMapper,
                      Function<Throwable, Future<V>> failureMapper);
    Future<T> recover(Function<Throwable, Future<T>> mapper);
    <V> Future<V> transform(Function<AsyncResult<T>, Future<V>> mapper);
    <V> Future<T> eventually(Function<Void, Future<V>> mapper);
    CompletionStage<T> toCompletionState();
}


[[Category:Database]]
</syntaxhighlight>
[[Category:Redis]]
 
[[Category:Vert.x]]

2021年7月25日 (日) 08:21的版本

Future相关操作的设计

AsyncResult

AsyncResult表示一个异步操作的结果,这个结果要么是成功的,要么是失败的。

interface AsyncResult<T> {
    /* 获取异步结果,或者失败原因。倘若失败,那么结果为null */
    T getResult();
    Throwable cause();
    boolean succeeded();
    boolean failed();

    /* 对结果进行转换。如果成功,那么可以将成功的结果转换为另一种形式;否则可以得到一个替代的结果。*/
    <V> AsyncResult<V> map(Function<T, V> mapper);
    <V> AsyncResult<V> map(V value);
    <V> AsyncResult<V> mapEmpty();
    AsyncResult<T> otherwise(T value);
    AsyncResult<T> otherwise(Function<Throwable, T> mapper);
    AsyncResult<T> otherwiseEmpty();
}

Future

Future表示一个尚未完成的异步操作,并继承自AsyncResult

interface Future<T>
    extends AsyncResult<T> {
    boolean isComplete();
    Future<T> onComplete(Handler<AsyncResult<T>> handler);
    Future<T> onSuccess(Handler<T> handler);
    Future<T> onFailure(Handler<Throwable> handler);
    <V> Future<V> compose(Function<T, Future<V>> mapper);
    <V> Future<V> compose(Function<T, Future<V>> successMapper, 
                      Function<Throwable, Future<V>> failureMapper);
    Future<T> recover(Function<Throwable, Future<T>> mapper);
    <V> Future<V> transform(Function<AsyncResult<T>, Future<V>> mapper);
    <V> Future<T> eventually(Function<Void, Future<V>> mapper);
    CompletionStage<T> toCompletionState();
}