memcache 怎么存储的对象
memchache 将对象序列化后保存 memcahce将值序列化成字节数组,然后存储到缓存中。 如下例,我们将user对象序列化到文件a.txt中,同时将user保存到缓存中,通过比较文件和缓存中的值 发现,两者是一样的。 public class User implements Serializable { priv
memchache 将对象序列化后保存
memcahce将值序列化成字节数组,然后存储到缓存中。
如下例,我们将user对象序列化到文件a.txt中,同时将user保存到缓存中,通过比较文件和缓存中的值
发现,两者是一样的。
<code class=" hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Serializable</span>{</span> <span class="hljs-keyword">private</span> String name; <span class="hljs-keyword">private</span> String address; <span class="hljs-keyword">public</span> <span class="hljs-title">User</span>(String name, String address) { <span class="hljs-keyword">super</span>(); <span class="hljs-keyword">this</span>.name = name; <span class="hljs-keyword">this</span>.address = address; } }</code>
<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span>(String[] args) throws InterruptedException, ExecutionException, FileNotFoundException, IOException { MemcachedClient mcc = <span class="hljs-keyword">null</span>; <span class="hljs-keyword">try</span>{ <span class="hljs-comment">// 本地连接 Memcached 服务</span> mcc = <span class="hljs-keyword">new</span> MemcachedClient(<span class="hljs-keyword">new</span> InetSocketAddress(<span class="hljs-string">"127.0.0.1"</span>, <span class="hljs-number">11211</span>)); System.<span class="hljs-keyword">out</span>.println(<span class="hljs-string">"Connection to server sucessful."</span>); }<span class="hljs-keyword">catch</span>(Exception ex){ System.<span class="hljs-keyword">out</span>.println( ex.getMessage() ); } User u = <span class="hljs-keyword">new</span> User(<span class="hljs-string">"junwang"</span>,<span class="hljs-string">"qingdao city"</span>); ObjectOutputStream oos = <span class="hljs-keyword">new</span> ObjectOutputStream(<span class="hljs-keyword">new</span> FileOutputStream(<span class="hljs-keyword">new</span> File(<span class="hljs-string">"a.txt"</span>)) ); oos.writeObject(u); Future fo = mcc.<span class="hljs-keyword">set</span>(<span class="hljs-string">"myuser"</span>, <span class="hljs-number">5</span>*<span class="hljs-number">60</span>*<span class="hljs-number">1000</span>, u); <span class="hljs-comment">// 查看存储状态</span> System.<span class="hljs-keyword">out</span>.println(<span class="hljs-string">"set status:"</span> + fo.<span class="hljs-keyword">get</span>()); <span class="hljs-comment">// 输出值</span> System.<span class="hljs-keyword">out</span>.println(<span class="hljs-string">"myuser value in cache - "</span> + mcc.<span class="hljs-keyword">get</span>(<span class="hljs-string">"myuser"</span>)); <span class="hljs-comment">// 关闭连接</span> mcc.shutdown(); }</code>
查看源代码
查看源代码,可以发现正是将对象序列化,然后保存。证明了我们上述的猜想。
<code class=" hljs cs">BaseSerializingTranscoder.java <span class="hljs-keyword">protected</span> <span class="hljs-keyword">byte</span>[] <span class="hljs-title">serialize</span>(Object o) { <span class="hljs-keyword">if</span> (o == <span class="hljs-keyword">null</span>) { <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> NullPointerException(<span class="hljs-string">"Can't serialize null"</span>); } <span class="hljs-keyword">byte</span>[] rv=<span class="hljs-keyword">null</span>; ByteArrayOutputStream bos = <span class="hljs-keyword">null</span>; ObjectOutputStream os = <span class="hljs-keyword">null</span>; <span class="hljs-keyword">try</span> { bos = <span class="hljs-keyword">new</span> ByteArrayOutputStream(); os = <span class="hljs-keyword">new</span> ObjectOutputStream(bos); os.writeObject(o); os.close(); bos.close(); rv = bos.toByteArray(); } <span class="hljs-keyword">catch</span> (IOException e) { <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException(<span class="hljs-string">"Non-serializable object"</span>, e); } <span class="hljs-keyword">finally</span> { CloseUtil.close(os); CloseUtil.close(bos); } <span class="hljs-keyword">return</span> rv; } </code>
结论
值的存储,都是序列化成字节数组,然后保存

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

This website reported on March 7 that Dr. Zhou Yuefeng, President of Huawei's Data Storage Product Line, recently attended the MWC2024 conference and specifically demonstrated the new generation OceanStorArctic magnetoelectric storage solution designed for warm data (WarmData) and cold data (ColdData). Zhou Yuefeng, President of Huawei's data storage product line, released a series of innovative solutions. Image source: Huawei's official press release attached to this site is as follows: The cost of this solution is 20% lower than that of magnetic tape, and its power consumption is 90% lower than that of hard disks. According to foreign technology media blocksandfiles, a Huawei spokesperson also revealed information about the magnetoelectric storage solution: Huawei's magnetoelectronic disk (MED) is a major innovation in magnetic storage media. First generation ME

How to enable the direct connection of the independent graphics card of the Shenzhou Xuanlong m7. To enable the direct connection function of the independent graphics card of the Shenzhou Xuanlong m7, you can follow the following steps: 1. First, make sure that you have installed the driver of the independent graphics card. You can go to the official Shenzhou website or the official website of the independent graphics card manufacturer to download and install the latest driver suitable for your graphics card model. 2. On the computer desktop, right-click a blank space and select "NVIDIA Control Panel" in the pop-up menu (if it is an AMD graphics card, select "AMDRadeon Settings"). 3. In the control panel, find "3D Settings" or a similarly named option and click to enter. 4. In "3D Settings" you need to find "Global Settings" or a similarly named option. Here you can specify the use of a unique

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Git is a fast, reliable, and adaptable distributed version control system. It is designed to support distributed, non-linear workflows, making it ideal for software development teams of all sizes. Each Git working directory is an independent repository with a complete history of all changes and the ability to track versions even without network access or a central server. GitHub is a Git repository hosted on the cloud that provides all the features of distributed revision control. GitHub is a Git repository hosted on the cloud. Unlike Git which is a CLI tool, GitHub has a web-based graphical user interface. It is used for version control, which involves collaborating with other developers and tracking changes to scripts and

As a world-renowned sports brand, Nike's shoes have attracted much attention. However, there are also a large number of counterfeit products on the market, including fake Nike shoe boxes. Distinguishing genuine shoe boxes from fake ones is crucial to protecting the rights and interests of consumers. This article will provide you with some simple and effective methods to help you distinguish between real and fake shoe boxes. 1: Outer packaging title By observing the outer packaging of Nike shoe boxes, you can find many subtle differences. Genuine Nike shoe boxes usually have high-quality paper materials that are smooth to the touch and have no obvious pungent smell. The fonts and logos on authentic shoe boxes are usually clear and detailed, and there are no blurs or color inconsistencies. 2: LOGO hot stamping title. The LOGO on Nike shoe boxes is usually hot stamping. The hot stamping part on the genuine shoe box will show

What is the resolution of Savior Y7000P when playing CF? The resolution of Savior Y7000P when playing CF is 1920*1080. Because this computer is equipped with a GTX1650 graphics card and an i5-9300H processor, its performance is relatively good and sufficient to meet the needs of games such as CF. At the same time, 1920*1080 is the current resolution of mainstream e-sports monitors, and the image quality and clarity are sufficient. In addition, if there are players with higher requirements, you can appropriately lower the game image quality settings to obtain a smoother gaming experience. In order to enjoy a clearer visual experience, you can adjust the resolution of the Savior y7000p to 2560*1400. This way, you will be able to enjoy higher quality image display. Equipped with the Savior Y7000P 2022 model

How to correctly use sessionStorage to store sensitive information requires specific code examples. Whether in web development or mobile application development, we often need to store and process sensitive information, such as user login credentials, ID numbers, etc. In front-end development, using sessionStorage is a common storage solution. However, since sessionStorage is browser-based storage, some security issues need to be paid attention to to ensure that the stored sensitive information is not maliciously accessed and used.
