# Two-Layer Network Caching for Different Service Requirements
## 3 Model
### Caching Problems
#### 3.3.3 Case 3: Guaranteed Download Time
```
1:functionAlgorithm3(\(\Xi,\Phi,\mathbf{P},K_{2}\), \(\{\lambda_{i}\}\),\(\{B_{j}\}\), \(b_{min}\), \(M\))\(\rightarrow\)\(\varepsilon_{j}\)
2:\(\varepsilon_{j}\leftarrow\varnothing\quad\forall j\)\(\triangleright\) initialization
3:\(step\gets 1\)
4:\(T_{1,j}\gets 0,\quad\forall j\)\(\triangleright\) counters of cache usage
5:\(T_{2,ij}\gets 0,\quad\forall i,j\)\(\triangleright\) counters of link usage
6:\(\mathbf{FB}\leftarrow\) [free link bandwidths]
7:\(d(m,i)\gets d(m,i)\lambda_{i}\quad\forall m,i\)
8:while\(\left(\exists j:T_{1,j}<\min\left(K_{2,j},\frac{B_{j}}{b_{min}}\right)\right) \wedge\left(\exists i:T_{2,i}<\frac{b_{ij}}{b_{min}}\right)\wedge\left(step \leq C\right)\)do
9:\(m^{*},\left\{j^{*}\right\}\leftarrow\)Procedure2 \((\Phi,\mathbf{D},\mathbf{T}_{1},\left|K2\right|,\Xi,\mathbf{FB})\)\(\triangleright\) see Code 5
10: step \(\leftarrow\) step+1
11:if\(T_{1,j^{*}}+1\leq\min\left(K_{2,j},\frac{B_{j}}{b_{min}}\right)\wedge T_{2,j^{*}} \leq b_{ij^{*}}\quad\forall i:\phi_{ij^{*}}=1\)then
12:\(\varepsilon_{j^{*}}=\varepsilon_{j^{*}}\cup\left\{m^{*}\right\}\)
13: cache \(m^{*}\) distributing items over \(\left\{j^{*}\right\}\)
14: update \(\mathbf{T}_{1}\) and \(\left[\mathbf{T}_{2}\right]\) and \(\mathbf{FB}\)
15:\(P(i,m^{*})\gets P(i,m^{*})\big{(}1-\Phi^{T}(\left\{j^{*}\right\},i)\big{)} \forall i\in\left[1,W\right]\)
16:else
17:\(\phi(i,j^{*})\gets 0\quad\forall i\)
18:endif
19:endwhile
20:return\(\varepsilon_{j}\)
21:endfunction
```  
**Code 4** Solution to Problem 6. ```
1:functionProcedure2(\(\phi,\mathbf{D},\mathbf{T}_{1},\left|K_{2}\right|,\Xi,\mathbf{FB}\))\(\rightarrow\)\(\xi^{*},\left\{j^{*}\right\}\)
2:\(\{m^{+}\}\leftarrow\varnothing,\{m^{*}\}\leftarrow\varnothing\)
3:\(\mathrm{Y}=\Phi^{T}\mathbf{D}\)
4:while\(\mathrm{Y}\
eq\left[0\right]\)do
5:\(\{m^{+}\}\leftarrow\)\(\max_{m}\mathrm{Y}\)
6:for all\(m\in\{m^{+}\}\)do
7:\(L_{m}\gets 0\)\(\triangleright\) space available for content \(m\) in caches
8:for all\(j\in\left\{j:(j,m^{+})=max(\mathrm{Y})\right\}\)do
9:\(L_{m}\gets L_{m}+K_{2,j}-T_{1,j}\)
10:endfor
11:if\(\left(L_{m}\geq size(m)\right)\wedge\left(\sum_{j\in\left\{j,m^{*}\right\}} \mathbf{FB}(\cdot,j)\geq b_{min}\right)\)then
12:\(\{m^{*}\}\gets m\)
13:endif
14:\(m^{*}=\) max-size \(\{m^{*}\}\)
15:\(\{j^{*}\}\leftarrow\{j:(j,m^{*})=\max\big{(}\Phi^{T}\mathbf{D}\big{)}\}\)
16:return\(m^{*},\{j^{*}\}\)
17:if\(\{m^{*}\}=\varnothing\)then
18:\(\mathrm{Y}(m^{+},\{j\in(j,m^{+})\})\gets 0\)\(\triangleright\) decrease the max \(\mathrm{Y}\) values of \(\{m^{+}\}\)
19:\(newmax\leftarrow\max_{m}\mathrm{Y}(m^{+},:)\)\(\triangleright\) columns to the second ones
20:\(\mathrm{Y}(m^{+},\{j\in(j,m^{+})\})\leftarrow\)\(newmax\)
21:endif
22:endfor
23:endwhile
24:return\(\varnothing,\varnothing\)\(\triangleright\) no solution found
25:endfunction
```  
**Code 5** Procedure used in Algorithm3. ```
1:functionProcedure2(\(\phi,\mathbf{D},\mathbf{T}_{1},\left|K_{2}\right|,\Xi,\mathbf{FB}\))\(\rightarrow\)\(\xi^{*},\left\{j^{*}\right\}\)
2:\(\{m^{+}\}\leftarrow\varnothing,\{m^{*}\}\leftarrow\varnothing\)
3:\(\mathrm{Y}=\Phi^{T}\mathbf{D}\)
4:while\(\mathrm{Y}\
eq\left[0\right]\)do
5:\(\{m^{+}\}\leftarrow\)\(\max_{m}\mathrm{Y}\)
6:for all\(m\in\{m^{+}\}\)do
7

#### 3.3.4 Case 4: Random Connections

In this case, in addition to the constraints above, we assume that each logical link is associated with a bandwidth ranging between 0 and a maximum value. In this view, the presence of a content in two caches accessible by the same user is not a redundancy but a solution to have the content accessible with the desired probability. A simple approach for introducing an effective caching system in this environment is to guarantee the presence of elements in caches _in expectation_. This problem has a formulation and a corresponding solution that can be derived from the problems already analyzed by replacing the \(\phi_{i,j}\) binary values with probability values of active connections between users and caches.