在Java中,使用内存中的高速缓存访问单个请求的服务响应中的变量是一种好习惯。还是只为该请求维护一个服务上下文对象并使用该对象访问所需的变量会更好。
内存缓存中是否没有延迟开销来检索响应对象?
例如:
服务上下文对象:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<form id='ui'>
<button class="btn btn-primary float-right" type="button" data-toggle="collapse" data-target="#colA">
Table Tools
</button>
<header class="collapse" id="colA">
<aside class="card card-body" style='padding:0;line-height:1;'>
<label class="input-group mb-3" for='rowUI'>
<input id='rowUI' type="number" class="form-control" placeholder="Enter Number of Rows" min='1' max='100' style='text-align:center;'>
<label class="input-group-append" for='rowBtn'>
<button id="rowBtn" class="btn btn-warning" type="button" >Generate Rows</button>
</label>
</label>
</aside>
<button id='delData' class="btn btn-danger float-right" type='button'>
Delete Data
</button>
<button id='xtcData' class="btn btn-info float-right" type='button'>
Extract Data
</button>
<button id='fillTab' class="btn btn-success float-right" type='button'>
Fill Table
</button>
</header>
<table id="gridTable" class="table table-bordered table-hover table-sm">
<thead>
<tr>
<th style="width:5%;"><input class="chkAll" type="checkbox"></th>
<th style="min-width: 60%;">Size</th>
<th style="width: 30%;">Quantity</th>
<th class="hidden"></th>
</tr>
</thead>
<tbody id="gridCore" class="table-secondary">
<tr class="gridRow">
<td><input type="checkbox"></td>
<td>
<select class="form-control form-control-sm size" name="size">
<option value=''>Pick a Size</option>
<option value='S'>Small</option>
<option value='M'>Medium</option>
<option value='L'>Large</option>
</select>
</td>
<td>
<input type="number" class="form-control form-control-sm qty" name="qty" min='1' max='600' style='text-align:center'>
</td>
<td class="hidden"></td>
</tr>
</tbody>
</table>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
使用内存缓存:
public class ServiceResponseAccessor {
private ServiceResponse response;
public String getCustomerId() {
return response.getCustomerId();
}
public Preferences getCustomerPreferences() {
Customer customer = response.getCustomer();
if (customer == null) {
throw new Exception("No Customer Preferences");
}
return customer.getPreferences();
}
}